Entity Framework 5 on .NET 4.0 - DatabaseGeneratedOption.Identity is undefined

风格不统一 提交于 2019-12-18 03:20:30

问题


I need to use EF5 on .NET 4 and I've run into a reference issue when mapping my class with HasDatabaseGenerationOption.Identity which doesn't exist in the 4.0 version of the library.

The following is failing:

this.Property(t => t.DeploymentLogId)
              .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

Does anyone know of a work around?


回答1:


Using NuGet to add EntityFramework to a project that targets .NET 4.5, will add EntityFramework 5.0.

If you later change the project to target .NET 4.0, EntityFramework 5.0 is still referenced.

To fix it, use NuGet to uninstall EntityFramework and add it back, also in NuGet. This will add EntityFramework 4.4 which is the last supported version for .NET 4.0.

If it still does not work there may be some references to the specific EF version in App.config. These can be removed.




回答2:


The namespace changed in EF 5.0. Try adding this:

using System.ComponentModel.DataAnnotations.Schema;



回答3:


Have you tried using a data annotation?

public class DeploymentLog
{
   [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int DeploymentLogId { get; set; }



回答4:


System.ComponentModel.DataAnnotations.Schema has only been a part of the .NET Framework since 4.5

If you're using 4.0 then Entity Framework will provide it for you. If you look at the source code of DatabaseGeneratedOption and the other files, you'll see that their code is wrapped in a conditional

#if NET40
...
#endif



回答5:


We had that problem very recently on an old project, and what we did was just

  • delete the EntityFramework reference in the project
  • right click on the project, do a Manage NuGet packages, go to the Updates category, and click Update on the Entity Framework item that was there in the list to version 6


来源:https://stackoverflow.com/questions/12183944/entity-framework-5-on-net-4-0-databasegeneratedoption-identity-is-undefined

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!