Solved

Must my class libraries reference Entity Framework to use data annotations on POCOs?

Posted on 2016-09-17
5
47 Views
Last Modified: 2016-09-17
I have a .net solution I'm creating to support generating postal labels for orders.  The orders are coming from eBay with payments from Paypal.  The postage service used is Royal Mail Post.  As I may reuse the the eBay, Paypal and Royal Mail items in future solutions it made sense for me to create five projects:

  • MVC Application -  provide user interface
  • Core class libary - Contains shared classes like Country, Currency, etc
  • Royal Mail class library
  • eBay class library
  • Paypal class library

I've placed the POCOs in the appropriate class library.  For example, Royal Mail class library has POCOs  Shipment, PostageType, etc.  The MVC application has an IdentityDbContext class declaring DBSets for the POCOs from the referenced class libraries.  

To my thinking the MVC app is the only project which should have the Entity Framework referenced.   The issue I have is I need to add the data annotations to the POCOs; such as Key, Index, etc.  Does this mean I need to reference EF in each of the class libraries?  I thought about using Fluent API but I want the annotations  in the class libraries so that they can be seen by other solutions  using the class library.

Thanks for any help!

Cheers,

Scott
0
Comment
Question by:canuckconsulting
  • 3
  • 2
5 Comments
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 41803170
In order to add DataAnnotations to properties in a class that project needs to have a reference to the dll System.ComponentModel.DataAnnotations.dll and you should add a using statement to System.ComponentModel.DataAnnotations.
0
 

Author Comment

by:canuckconsulting
ID: 41803179
Thanks Fernando.

I have that reference and have using statements to  both System.ComponentModel.DataAnnotations and System.ComponentModel.DataAnnotations.Schema.  The problem is that if I don't have entity framework I get an error on the Index data attribute.  The others work fine but  an annotation like the one below fails:

        [Index(IsUnique = true)]
0
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 41803198
Hi canuckconsulting;

The IndexAttribute also needs a reference to the EntityFramework.dll using the namespace  System.ComponentModel.DataAnnotations.Schema as you already have a using statement for. This can be verified in the documentation for IndexAttribute Class.
0
 

Author Closing Comment

by:canuckconsulting
ID: 41803203
Thanks!
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 41803205
Not a problem canuckconsulting, glad to help.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Question! 4 36
REXEX help Part 2 2 25
Sending receiving text messages in vb.net 15 35
Winforms -- Capture CTRL+SHIFT+D at the form not working? 3 22
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question