Error Key Already Exists in Table when scaffolding

2020-05-23 04:57发布

问题:

I am trying to follow the Music Store Example in Professional MVC 4 using VS2015. I am having issues scaffolding the music store controller. Everytime I try to create the controller a Error window pops up and the only information in it is: "There was an error running the selected code generator:'Key already exists in table.'"

I have searched around for this specific errors but most of the scaffolding error solutions seem to be about errors in the web.config, yet nothing has even changed in my web.config, it is the default one created when the new project was created.

I have tried creating another MVC project and coding the models again yet I still receive the error.

I am using Microsoft Visual Studio Enterprise 2015 version 14.0.247200 Update 1 if that helps.

The Classes that I have created in the Models folder are as follows and are exactly as they are in the book:

public class Album
{
    public virtual int AlbumId { get; set; }
    public virtual int GenreId { get; set; }
    public virtual int ArtistId { get; set; }
    public virtual string Title { get; set; }
    public virtual decimal Price { get; set; }
    public virtual string AlbumArtUrl { get; set; }
    public virtual Genre Genre { get; set; }
    public virtual Artist Artist { get; set; }
}

public class Artist
{
    public virtual int ArtistId { get; set; }
    public virtual string Name { get; set; }
    public virtual string Description { get; set; }
}

public class Genre
{
    public virtual int GenreId { get; set; }
    public virtual string Name { get; set; }
    public virtual string Description { get; set; }
    public virtual List<Album> Albums { get; set; }
}

Thanks for your help

回答1:

I had the same problem. I'm using Visual Studio Communitty 2015. Try a Model Class with the code:

public class MusicStoreDB : DbContext
{
    public MusicStoreDB() : base("name=MusicStoreDB")
    {
    }

    public DbSet<Album> Albums { get; set; }
    public DbSet<Artist> Artists { get; set; }
    public DbSet<Genre> Genres { get; set; }
}

At the add controller window, use MusicStoreDB as the context.

Working code can be found here.

The code for Chapter 4 looks like it has been generated from database so MusicStoreDB class is slightly different.



回答2:

I had the same issues 'Key already exists in table.' in VS 2015 Pro and found that if I closed all the open windows of code, ran a clean and then a rebuild and tried the scaffolding option again, everything worked perfectly!



回答3:

Same issue for me on VS 2015 Community Edition.

I was able to get scaffolding to work only if I commented out the following in Album class:

public virtual Genre  Genre { get; set; }
public virtual Artist Artist { get; set; }

After scaffolding you can uncomment and fix up the controller and view.



回答4:

Earlier my colleagues was working with EF and it was highlighted that to make it work properly the packages and the version of the packages is very important.If not getting exactly right,various unrelated error messages may appear like the "Key already exists in table error" message when trying to use EF.

Since they are using it, i decided to read the wrox book Professional ASP.NET MVC 5 and was going through chapter 4 when i encountered the same error as you all.Anyway i downloaded their sample solution chapter 4 from

http://www.wrox.com/WileyCDA/WroxTitle/Professional-ASP-NET-MVC-5.productCd-1118794753,descCd-DOWNLOAD.html

as suggested and i copy the package folder from their sample and replace the package folder in my project and it works.The StoreManageController is created

Now please do feedback if this works for all.It worked for me.I am using VS2015 Professional



回答5:

Are you sure every single Property is marked as virtual in the samples? I don't see how it makes any sense for Name, Price, etc to be navigation properties.

Try the following, setting only navigation properties (things with relationships) to virtual.

public class Album
{
    public int AlbumId { get; set; }
    public string Title { get; set; }
    public decimal Price { get; set; }
    public string AlbumArtUrl { get; set; }
    public int GenreId { get; set; }
    public virtual Genre Genre { get; set; }
    public int ArtistId { get; set; }
    public virtual Artist Artist { get; set; }
}

public class Artist
{
    public int ArtistId { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
}

public class Genre
{
    public int GenreId { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public virtual List<Album> Albums { get; set; }
}


回答6:

I had the same issue and resolved it by selecting for "Data context class:" the context from {ConnectionName}Entities ({MyProject}.Models) vice ApplicationDbContext ({MyProject}.Models).



回答7:

Remove the foreign key properties:

public virtual int GenreId { get; set; }
public virtual int ArtistId { get; set; }


回答8:

In my case this was caused by selecting incorrect Data context class in the dialog "Add Controller". My project contented 2 data context classes and I was creating a new controller for a model class which already used in another data context class. Change of data context class solved this error( Key already exists... ) in my case. Cleaning, rebuilding, restarting VS(VS 2015 Pro ) didn't help.



回答9:

I too faced this issue with my model. I just added [ForeignKey("")] attribute to the key to make it work.