Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?)

like image 507
ahmadali shafiee Avatar asked Jul 02 '12 20:07

ahmadali shafiee


3 Answers

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

like image 142
Erik Funkenbusch Avatar answered Nov 12 '22 13:11

Erik Funkenbusch


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.

like image 24
Mahmood Dehghan Avatar answered Nov 12 '22 12:11

Mahmood Dehghan


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

like image 1
d4c0d312 Avatar answered Nov 12 '22 12:11

d4c0d312