Why `DatabaseGenerated(DatabaseGeneratedOption.Identity)` doesn't work in MVC 4

主宰稳场 提交于 2019-12-08 16:29:29

问题


I was trying to move my MVC 3 project to MVC 4 but when I wanted to move this model:

public class Link
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Guid ID { get; set; }

    [DisplayName("Shorted URL")]
    public string SURL { get; set; }

    [DisplayName("General Link")]
    public string OriginalURL { get; set; }

    [DisplayName("Click Count")]
    public int ClickCount { get; set; }
}

public class LinkDBContext : DbContext
{
    public DbSet<Link> Links { get; set; }
}

I got error with [System.ComponentModel.DataAnnotations.(DatabaseGeneratedOption.Identity)] attribute. I don't know what's the problem. Does anyone know?!?

Update

These are the errors:

The type or namespace name 'DatabaseGeneratedAttribute' could not be found (are you missing a using directive or an assembly reference?)

The type or namespace name 'DatabaseGenerated' could not be found (are you missing a using directive or an assembly reference?)


回答1:


DatabaseGeneratedAttribute is in the System.ComponentModel.DataAnnotations.Schema namespace attribute in .NET 4.5




回答2:


If you want to use this attribute in .net 4 you can use Prerelease version of EntityFramework 6 (or even Nightly Builds) to do this, in Manage NuGet Pakages window, from the drop-down on top of the window, select Include Prerelease.

To update to Nightly Builds, in Pakage Manager Settings add this Package Source:

http://www.myget.org/F/aspnetwebstacknightly/

For a complete guide, see EF on GitHub.




回答3:


You need - in some cases - to change the framework from 4.5 or less to 4.5.1 and then install Entity Framework 6 + and it will be found



来源:https://stackoverflow.com/questions/11300883/why-databasegenerateddatabasegeneratedoption-identity-doesnt-work-in-mvc-4

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