Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF data annotations aren't "working" after targeting .NET 4.5

I've got a project that was targeting .NET 4.0 and using EF 5.0. After changing the target framework to 4.5 (and updating the EF 5.0 reference to use the .NET 4.5 assembly), it seems like the data annotations aren't working anymore. For example:

[Table("ApplicationSession", Schema = "Application")]
public class ApplicationSessionEntity
{
    [Key, ForeignKey("GenericSession")]
    public int GenericSessionID { get; set; }
...

used to work fine, but now at runtime, the DbContext throws an InvalidOperationException: Unable to determine the principal end of an association....

I can add the Fluent api calls to resolve this (and it does), but then it doesn't recognize that the table is not in the "dbo" schema. Again, I know that the Fluent api can be used to resolve this, but why are the data annotations suddenly being ignored?

Thanks!

like image 228
Rob Avatar asked Jan 04 '13 20:01

Rob


1 Answers

In .NET Framework 4.5 EF annotations were moved from EF.dll to System.ComponentModel.Annotations assembly. It seems that even though you target .NET Framework 4.5 you still have a reference to EntityFramework.dll v4.4.0.0 somewhere. As a result your classes are compiled with attributes from the 4.4.0.0 assembly. At runtime the newer EntityFramework.dll (5.0.0.0) is being used and it looks for attributes from System.ComponentModel.DataAnnotations assembly. Those cannot be found since you have the ones from EF.dll 4.4.0.0 and therefore it looks like attributes are being ignored.

like image 148
Pawel Avatar answered Sep 24 '22 20:09

Pawel