【发布时间】:2018-11-22 03:46:23
【问题描述】:
我正在尝试填充流派表,但不断收到错误:
列名无效
我有一个简单的电影类型类模型。
public class Genre
{
public int Id { get; set; }
public string Name { get; set; }
}
一个电影类与这样的类型相关联:
public class Movie
{
public int Id { get; set; }
[Required]
public string Name { get; set; }
[Required]
public Genre Genre { get; set; }
[Required]
public DateTime ReleaseDate { get; set; }
[Required]
public DateTime DateAdded { get; set; }
[Required]
public int NumInStock { get; set; }
}
在我的 Nuget 控制台中,我运行 add-migration,它为我生成了一个空的 Genre 表,其中包含两列 Id 和 name。
然后,我尝试使用以下 SQL 查询填充流派表:
public override void Up()
{
Sql("INSERT INTO Genres (Id, Name) VALUES (1, Fantasy)");
Sql("INSERT INTO Genres (Id, Name) VALUES (2, Drama)");
Sql("INSERT INTO Genres (Id, Name) VALUES (3, Action-Adventure)");
Sql("INSERT INTO Genres (Id, Name) VALUES (4, Foreign)");
Sql("INSERT INTO Genres (Id, Name) VALUES (5, Horror)");
Sql("INSERT INTO Genres (Id, Name) VALUES (6, Romance)");
Sql("INSERT INTO Genres (Id, Name) VALUES (7, Crime)");
Sql("INSERT INTO Genres (Id, Name) VALUES (8, Thriller)");
Sql("INSERT INTO Genres (Id, Name) VALUES (9, Animated)");
Sql("INSERT INTO Genres (Id, Name) VALUES (10, Western)");
}
不知道我做错了什么。如果我将 'fantasy' 放在引号中(因为它可能需要一个字符串?)然后我会收到这个错误:
当 IDENTITY_INSERT 设置为 OFF 时,无法为表“流派”中的标识列插入显式值。
【问题讨论】:
-
检查你的
Genres表的定义,Id是一个系统生成的列Identity column,没有指定SET INDENTITY_INSERT到ON,你不能在这个列中插入你自己的值,即使它们是相同的。
标签: c# asp.net sql-server linq