[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

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

Posted on 2016-09-17
5
Medium Priority
?
86 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 64

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 64

Accepted Solution

by:
Fernando Soto earned 2000 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 64

Expert Comment

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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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 …
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

650 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