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

asp.net-mvc-migration c# ef-code-first entity-framework

Question

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
11
12/29/2016 4:17:30 PM

Accepted Answer

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

25
7/2/2012 9:35:00 PM

Popular Answer

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.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow