ef6 icon indicating copy to clipboard operation
ef6 copied to clipboard

Entity Framework 6.4.4 ignores data annotation attributes from entities defined in another project

Open SetTrend opened this issue 6 years ago • 8 comments

Entity Framework 6.4.4 ignores data annotation attributes from entities defined in another project.

Synopsis: To be able to omit Entity Framework (v6.4.4) dependency in my unit tests I created a second project containing all my entities and have my Entity Framework project reference the other project and the entities therein (see repository below).

When I then have Entity Framework create the database, all data annotations are being ignored: For example, there is no NOT NULL column in any table. And all other constraints are missing.

Steps to reproduce:

Open below Visual Studio 2019 solution and run the CreateDb() test function in EF-Tests > CreateDbTests.


EF-ForeignDataAnnotations.zip

Visual Studio 2019 solution screenshot

SetTrend avatar Jul 15 '20 00:07 SetTrend

I am also seeing an issue like this with 6.4.x.

Our models and data context are in a class library, and are defined using a mixture of fluent API and data annotations. Since upgrading some NuGet packages in one of our .NET Core 3.1-based Azure Functions projects, Application Insights has been throwing errors like this:

One or more validation errors were detected during model generation:

Portal.Data.Pricing.MasterPrice: : EntityType 'MasterPrice' has no key defined. Define the key for this EntityType.
Portal.Data.Pricing.PricingMarkup: : EntityType 'PricingMarkup' has no key defined. Define the key for this EntityType.
MasterPrices: EntityType: EntitySet 'MasterPrices' is based on type 'MasterPrice' that has no keys defined.
PricingMarkup: EntityType: EntitySet 'PricingMarkup' is based on type 'PricingMarkup' that has no keys defined.

Both entities have a [Key] annotation, but the annotations are being ignored. The same for the [Table] annotation. Defining the table and key using fluent API resolves the issue.

Downgrading to 6.4.0 does not resolve the issue; and we have been running 6.4.4 for some time so I wasn't expecting that to change anything anyway.

I may be far off the mark, but the only data-related NuGet change I can see is that we upgraded from System.ComponentModel.Annotations 4.7.0 to 5.0.0. Could that be the cause?

AndyMDoyle avatar Nov 17 '20 14:11 AndyMDoyle

This issue didn't get any reception yet. Maybe it'd make sense to invite @ajcvickers and @bricelam to this thread?

SetTrend avatar Nov 17 '20 14:11 SetTrend

@SetTrend It looks like binding redirects aren't being added by NuGet/MSBuild. Add the following to your test project:

<PropertyGroup>
  <AutoGenerateBindingRedirects>True</AutoGenerateBindingRedirects>
  <GenerateBindingRedirectsOutputType>True</GenerateBindingRedirectsOutputType>
</PropertyGroup>

@AndyMDoyle You may be hitting an entirely different issue since binding redirects aren't required on .NET Core...

bricelam avatar Nov 30 '20 23:11 bricelam

Thanks, @bricelam, for helping us out on this!

SetTrend avatar Dec 01 '20 14:12 SetTrend

please check out

https://github.com/dotnet/ef6/issues/1930

please make some noise about this so that it actually gets fixed as this is extremely annoying

@SetTrend

Seabizkit avatar Dec 22 '21 14:12 Seabizkit

@bricelam: Is something on your roadmap that's going to resolve these kind of issues?

SetTrend avatar Dec 22 '21 15:12 SetTrend

Just commented on #1930 that I think this is caused by mismatches between using System.ComponentModel.Annotations nuget pack and using the one in the GAC.

You have to use one or the other across all your projects.

rbonini-doit avatar Mar 02 '22 14:03 rbonini-doit

This issue has been closed because EF6 is no longer being actively developed. We are instead focusing on stability of the codebase, which means we will only make changes to address security issues. See the repo README for more information.

ajcvickers avatar Apr 25 '22 09:04 ajcvickers