Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 脚手架控制器vs2015运行时,表中已存在错误键_C#_Asp.net Mvc_Entity Framework_Visual Studio_Visual Studio 2015 - Fatal编程技术网

C# 脚手架控制器vs2015运行时,表中已存在错误键

C# 脚手架控制器vs2015运行时,表中已存在错误键,c#,asp.net-mvc,entity-framework,visual-studio,visual-studio-2015,C#,Asp.net Mvc,Entity Framework,Visual Studio,Visual Studio 2015,我正在尝试使用VS2015在Professional MVC 4中学习音乐商店的例子。我的音乐商店控制器有问题。每次我尝试创建控制器时,都会弹出一个错误窗口,其中唯一的信息是:“运行所选代码生成器时出错:'表中已存在键'。” 我已经搜索过这个特定的错误,但是大多数脚手架错误解决方案似乎都是关于web.config中的错误,但是我的web.config中甚至没有任何更改,它是在创建新项目时创建的默认错误 我曾尝试创建另一个MVC项目并再次编码模型,但仍然收到错误 我正在使用Microsoft Vi

我正在尝试使用VS2015在Professional MVC 4中学习音乐商店的例子。我的音乐商店控制器有问题。每次我尝试创建控制器时,都会弹出一个错误窗口,其中唯一的信息是:“运行所选代码生成器时出错:'表中已存在键'。”

我已经搜索过这个特定的错误,但是大多数脚手架错误解决方案似乎都是关于web.config中的错误,但是我的web.config中甚至没有任何更改,它是在创建新项目时创建的默认错误

我曾尝试创建另一个MVC项目并再次编码模型,但仍然收到错误

我正在使用Microsoft Visual Studio Enterprise 2015版本14.0.247200更新1(如果有帮助)

我在Models文件夹中创建的类如下所示,与书中的类完全相同:

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; }
}
公共类相册
{
公共虚拟int AlbumId{get;set;}
公共虚拟整数GenreId{get;set;}
公共虚拟整数ArtistId{get;set;}
公共虚拟字符串标题{get;set;}
公共虚拟十进制价格{get;set;}
公共虚拟字符串AlbumArtUrl{get;set;}
公共虚拟类型{get;set;}
公共虚拟艺术家{get;set;}
}
公共级艺术家
{
公共虚拟整数ArtistId{get;set;}
公共虚拟字符串名称{get;set;}
公共虚拟字符串描述{get;set;}
}
公共课体裁
{
公共虚拟整数GenreId{get;set;}
公共虚拟字符串名称{get;set;}
公共虚拟字符串描述{get;set;}
公共虚拟列表相册{get;set;}
}

感谢您的帮助

您确定示例中的每个属性都标记为
虚拟
?我不认为把
名称
价格
等作为导航属性有什么意义

尝试以下操作,仅将导航属性(具有关系的对象)设置为虚拟

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; }
}
公共类相册
{
public int AlbumId{get;set;}
公共字符串标题{get;set;}
公共十进制价格{get;set;}
公共字符串AlbumArtUrl{get;set;}
public int GenreId{get;set;}
公共虚拟类型{get;set;}
公共int ArtistId{get;set;}
公共虚拟艺术家{get;set;}
}
公共级艺术家
{
公共int ArtistId{get;set;}
公共字符串名称{get;set;}
公共字符串说明{get;set;}
}
公共课体裁
{
public int GenreId{get;set;}
公共字符串名称{get;set;}
公共字符串说明{get;set;}
公共虚拟列表相册{get;set;}
}

我也有同样的问题。我正在使用Visual Studio Communitty 2015。 尝试使用以下代码创建模型类:

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:DbContext
{
public MusicStoreDB():base(“name=MusicStoreDB”)
{
}
公共数据库集


第4章的代码看起来像是从数据库生成的,所以MusicStoreDB类略有不同。

我也遇到了同样的问题,并通过选择“数据上下文类”解决了它:{ConnectionName}实体({MyProject}.Models)的上下文,而不是ApplicationDbContext({MyProject}.Models).

与我在VS 2015社区版上的版本相同

只有在Album类中注释掉以下内容,我才能让脚手架工作:

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

搭建之后,您可以取消注释并修复控制器和视图。

删除外键属性:

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

早些时候,我的同事正在使用EF,他们强调,要使其正常工作,软件包和软件包的版本非常重要。如果没有完全正确,在尝试使用EF时,各种不相关的错误消息可能会显示为“表中已存在键错误”消息

由于他们正在使用它,我决定阅读wrox book Professional ASP.NET MVC 5,并在阅读第4章时遇到了与大家相同的错误

按照建议,我从他们的示例中复制了包文件夹,并替换了项目中的包文件夹,它就可以工作了


现在,如果这对所有人都有效,请提供反馈。它对我有效。我正在使用VS2015 Professional

我的模型也面临着这个问题。我刚刚添加了[ForeignKey(“”)属性设置为键以使其工作。

在VS 2015 Pro中,我遇到了相同的问题“表中已存在键”。我发现,如果我关闭所有打开的代码窗口,运行清理,然后重新生成,并再次尝试脚手架选项,一切都很好!

在我的情况下,这是由于在表中选择了不正确的数据上下文类造成的对话框“添加控制器”。我的项目包含2个数据上下文类,我正在为已在另一个数据上下文类中使用的模型类创建一个新控制器。数据上下文类的更改解决了此错误(密钥已存在…)。在我的情况下。清理、重建、重新启动VS(VS 2015 Pro)没有帮助。

您在尝试创建新项目时是否使用了新文件夹?当VS尝试生成的文件已经存在于文件系统中时,我看到了类似的错误。在我看来,ASP.NET和MVC目前正在经历根本性的变化,MVC4甚至MVC5上的所有现有书籍似乎都使用VS 2013或更早版本obably应该等待有关MVC6的书籍,以了解这些内容并避免沮丧。为什么VS 2015社区中创建的MVC 5项目在诸如当前书籍(如Pro ASP.NET MVC 5)中的MVC 5代码中的脚手架代码等方面存在问题?有什么变化?附议。他们应该有一个核心教程,并确保对其进行修订和修改随着平台的发展,让他们继续工作。否则,你会通过跟踪某个东西获得25%的收益,并达到一个让你漂泊不定的程度。是的