canuckconsulting
asked on
Must my class libraries reference Entity Framework to use data annotations on POCOs?
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:
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
- 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
In order to add DataAnnotations to properties in a class that project needs to have a reference to the dll System.ComponentModel.Data Annotation s.dll and you should add a using statement to System.ComponentModel.Data Annotation s.
ASKER
Thanks Fernando.
I have that reference and have using statements to both System.ComponentModel.Data Annotation s and System.ComponentModel.Data Annotation s.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)]
I have that reference and have using statements to both System.ComponentModel.Data
[Index(IsUnique = true)]
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks!
Not a problem canuckconsulting, glad to help.