本文介绍了脚手架控制器vs2015时出错键已经存在于表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想跟着音乐商店实例专业MVC 4使用VS2015。我有脚手架的音乐存储控制器的问题。每次我试图创建一个控制器中的错误窗口弹出,并在它的唯一信息是:有运行选定code产生一个​​错误:'键已经存在于表'。

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.'"

我已经围绕搜索此特定错误,但大部分的脚手架错误的解决方案似乎是有关在web.config中的错误,但没有在我的web.config甚至改变了,这是创建的默认之一,当新项目已创建。

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.

我曾尝试创建另一个MVC项目,然后重新编码模型但我仍然收到错误。

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

我使用Microsoft Visual Studio企业版2015更新14.0.247200 1是否有帮助。

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; }
}

感谢您的帮助。

推荐答案

我有同样的问题。我使用Visual Studio 2015年Communitty。
尝试模型类的code:

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; }
}

在添加控制器窗口中,使用MusicStoreDB作为背景。

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

工作code可以发现here.

Working code can be found here.

在code为第4章看起来像它已经从数据库中生成这样MusicStoreDB类略有不同。

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

这篇关于脚手架控制器vs2015时出错键已经存在于表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 19:32