Solved

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

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

Expert Comment

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

Featured Post

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Form Data Disappears Using Back Button 4 66
Convert a string to date 4 40
.NET XML to Dictionary - XML has repeating node names 8 28
VB .net 2010 Byte array 2 21
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.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

734 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