11

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

Stacked
  • 6,892
  • 7
  • 57
  • 73
ahmadali shafiee
  • 4,350
  • 12
  • 56
  • 91
  • 6
    First, your problem is unrelated to MVC (either 3 or 4), as your code is dealing with Entity Framework. Second, you don't say what version of EF you're using, and you don't say what error you recieved. Since my psychic powers are not working today, you will have to try be more specific. – Erik Funkenbusch Jul 02 '12 at 21:20
  • @MystereMan I just created a Internet application using VS2012 and it has EF itself. I don't know what version is! – ahmadali shafiee Jul 02 '12 at 21:21
  • 1
    If you can include the actual error message in the question that would be helpful. – Satish Jul 02 '12 at 21:23
  • 1
    I would suggest learning a little more about the tools you're using. FYI, it's EF 5, and C# 5, not C#4. You can find all this out by just looking at the installed NuGet packages. – Erik Funkenbusch Jul 02 '12 at 21:25
  • 2
    question edited @MystereMan: I didn't know that the version is needed!!! and it's **`.Net Framework 4.5`** not `.Net Framework 5`!!! – ahmadali shafiee Jul 02 '12 at 21:27
  • I cannot see any `Ef Code First` in Nuget package manage. but `EntityFramework` version is 5.0.0-rc – ahmadali shafiee Jul 02 '12 at 21:35
  • @ahmadalishafiee - I did not say it was .Net Framework 5. I said it's C# 5 and EF5, both of which work with .NET Framework 4.5 – Erik Funkenbusch Jul 02 '12 at 21:35
  • @ahmadalishafiee - There is no such thing as EF Code First package. Entity Framework is just that, and supports DatabaseFirst, ModelFirst AND CodeFirst. – Erik Funkenbusch Jul 02 '12 at 21:36

3 Answers3

28

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

Erik Funkenbusch
  • 92,674
  • 28
  • 195
  • 291
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.

Stacked
  • 6,892
  • 7
  • 57
  • 73
Mahmood Dehghan
  • 7,761
  • 5
  • 54
  • 71
1

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

d4c0d312
  • 748
  • 8
  • 24
  • Thanks! This worked for me, although I didn't need to install EF6 since I already had a version of EF in my project. – mkimmet Jul 01 '16 at 13:12