Solved

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

Posted on 2016-09-17
5
40 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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

747 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now