我正在尝试使用VS2015关注专业MVC 4中的音乐商店示例。我有脚手架音乐商店控制器的问题。每次我尝试创建控制器时,都会弹出一个错误窗口,其中唯一的信息是:“运行所选代码生成器时出错:'表中已存在密钥。'”
我已经搜索过这个特定的错误但是大多数脚手架错误解决方案似乎是关于web.config中的错误,但是我的web.config中没有任何改变,它是新项目时创建的默认错误创建了。
我尝试过创建另一个MVC项目并再次对模型进行编码,但仍然收到错误。
如果有帮助,我使用的是Microsoft Visual Studio Enterprise 2015版本14.0.247200 Update 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; }
}
感谢您的帮助
答案 0 :(得分:18)
我遇到了同样的问题。我正在使用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作为上下文。
可以找到工作代码here。
第4章的代码看起来像是从数据库生成的,所以MusicStoreDB类略有不同。
答案 1 :(得分:4)
我遇到了同样的问题'表中已存在密钥'。在VS 2015 Pro中发现如果我关闭所有打开的代码窗口,运行一个干净然后重建并再次尝试脚手架选项,一切都运行完美!
答案 2 :(得分:2)
VS 2015 Community Edition上的问题相同。
只有在Album类中注释掉以下内容时,我才能使脚手架工作:
public virtual Genre Genre { get; set; }
public virtual Artist Artist { get; set; }
脚手架后,您可以取消注释并修复控制器和视图。
答案 3 :(得分:1)
早些时候,我的同事正在与EF合作,并强调要使其正常工作,软件包和软件包的版本非常重要。如果没有完全正确,各种不相关的错误消息可能会显示为&#34;密钥已存在于表错误&#34;尝试使用EF时的错误消息。
由于他们正在使用它,我决定阅读wrox书籍Professional ASP.NET MVC 5,当我遇到与你一样的错误时,我正在阅读第4章。无论如何,我从
按照建议,我从他们的示例复制包文件夹并替换我的项目中的包文件夹,它的工作原理。创建了StoreManageController
现在请反馈,如果这适用于所有人。它对我有用。我正在使用VS2015专业
答案 4 :(得分:0)
您确定样本中的每个属性都标记为virtual
吗?我看不出Name
,Price
等导航属性是否合理。
尝试以下操作,仅将导航属性(带关系的东西)设置为虚拟。
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; }
}
答案 5 :(得分:0)
我有同样的问题并通过选择&#34;数据上下文类来解决它:&#34;来自{ConnectionName}实体的上下文({MyProject} .Models)副ApplicationDbContext({MyProject} .Models)。
答案 6 :(得分:0)
删除外键属性:
public virtual int GenreId { get; set; }
public virtual int ArtistId { get; set; }
答案 7 :(得分:0)
在我的情况下,这是由于在“添加控制器”对话框中选择了错误的数据上下文类引起的。我的项目满足了2个数据上下文类,我正在为已经在另一个数据上下文类中使用的模型类创建一个新的控制器。在我的情况下,数据上下文类的更改解决了这个错误(Key已经存在...)。清理,重建,重启VS(VS 2015 Pro)没有帮助。
答案 8 :(得分:-1)
我的模型也遇到了这个问题。我刚刚将[ForeignKey(“”)]属性添加到键中以使其工作。