?
Solved

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

Posted on 2016-09-17
5
Medium Priority
?
78 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 63

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 63

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 63

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
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 …
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

752 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