简体   繁体   English

“当IDENTITY_INSERT设置为OFF时,无法为表'Movies'中的标识列插入显式值。”

[英]“Cannot insert explicit value for identity column in table 'Movies' when IDENTITY_INSERT is set to OFF.”

I'm using code first with entity framework. 我首先在实体框架中使用代码。 I have been getting the error below and can't figure out how to fix it: 我一直在下面的错误,无法弄清楚如何解决它:

"Cannot insert explicit value for identity column in table 'Movies' when IDENTITY_INSERT is set to OFF." “当IDENTITY_INSERT设置为OFF时,无法在表'Movies'中为身份列插入显式值。”

I've read that setting Sql("SET IDENTITY_INSERT Movies ON") and OFF around my migration query should fix this however I did not run any queries on the Movies table. 我已经读到,在迁移查询周围设置Sql(“ SET IDENTITY_INSERT Movies ON”)和OFF应该可以解决此问题,但是我没有在Movies表上运行任何查询。

Movies table: 电影表:

{
    public class Movies
    {
        public byte Id { get; set; }

        [Display (Name = "Movie Name")]
        public string MovieName { get; set; }

        public Genre Genre { get; set; }

        [Required]
        public byte GenreId { get; set; }

        [Display (Name = "Release Date")]
        public DateTime ReleaseDate { get; set; }

        public DateTime DateAdded { get; set; }

        [Display (Name = "Numbers in Stock")]
        public int NumberInStock { get; set; }
    }
}

My Movies controller: 我的电影控制器:

public ActionResult Save(Movies movies) {
        if (movies.Id == 0)
        {
            _context.Movies.Add(movies);
        }
        else {
            var moviesInDb = _context.Movies.Single(c => c.Id == movies.Id);
            moviesInDb.MovieName = movies.MovieName;
            moviesInDb.ReleaseDate = movies.ReleaseDate;
            moviesInDb.GenreId = movies.GenreId;
            moviesInDb.NumberInStock = movies.NumberInStock;
        }

        _context.SaveChanges();

        return RedirectToAction("Index, Movies");
    }

I am getting the error on _context.SaveChanges(); 我在_context.SaveChanges()上收到错误消息;

I do have queries for my Genre table which is as below 我确实有对我的流派表的查询,如下所示

public partial class PopulateGenreTable : DbMigration
{
    public override void Up()
    {            
        Sql("INSERT INTO Genres (Id, Name) VALUES (1, 'Action')");
        Sql("INSERT INTO Genres (Id, Name) VALUES (2, 'Thriller')");
        Sql("INSERT INTO Genres (Id, Name) VALUES (3, 'Family')");
        Sql("INSERT INTO Genres (Id, Name) VALUES (4, 'Romance')");
        Sql("INSERT INTO Genres (Id, Name) VALUES (5, 'Comedy')");            
    }

    public override void Down()
    {
    }
}

That's the only place I've seeded the dabase 那是我播种数据库的唯一地方

How do I fix this? 我该如何解决? Please explain clearly as I am an absolute beginner. 由于我是绝对的初学者,请清楚地说明。 Thanks 谢谢

You could easily avoid this error by not specifying the Id , when you insert a record to the Genres table. 当您在Genres表中插入记录时,可以通过不指定Id来轻松避免此错误。 As it seems Id is an IDENTITY COLUMN. 看来Id是一个IDENTITY栏。 Hence you don't have to specify a value when you insert there a record. 因此,在其中插入记录时无需指定值。 The database would generate always the correct Id value based on the IDENTITY you have defined. 数据库将根据您定义的IDENTITY始终生成正确的Id值。 Usually we have Columns of type INT and IDENTITY(1,1). 通常我们有INT和IDENTITY(1,1)类型的Columns。 That means that the first row that would be inserted would have as an Id the value of 1. The second row the value of 2 and so on and so forth. 这意味着将要插入的第一行的Id值为1,第二行的值为2,依此类推。

For further info please have a look here . 欲了解更多信息,请在这里看看。

Regarding the error message you get about to IDENTITY_INSERT... , the command SET IDENTITY_INSERT can be used when we want to insert one (or more) explicit value(s) to a column with an IDENTITY. 关于您将获得IDENTITY_INSERT...的错误消息,当我们想向具有IDENTITY的列插入一个(或多个)显式值时,可以使用命令SET IDENTITY_INSERT。 A detailed explanation on how to use this command you can find here . 您可以在此处找到有关如何使用此命令的详细说明。

You have to first turn it ON, then do your insert and then turn it OFF 您必须先将其打开,然后插入然后再将其关闭

Sql("SET IDENTITY_INSERT Genres ON");

Sql("INSERT INTO Genres (Id, Name) VALUES (1, 'Action')");
Sql("INSERT INTO Genres (Id, Name) VALUES (2, 'Thriller')");
Sql("INSERT INTO Genres (Id, Name) VALUES (3, 'Family')");
Sql("INSERT INTO Genres (Id, Name) VALUES (4, 'Romance')");
Sql("INSERT INTO Genres (Id, Name) VALUES (5, 'Comedy')");

Sql("SET IDENTITY_INSERT Genres OFF");

But, the point of having an identity column is that we don't choose what values the identity holds (read up on surrogate keys). 但是,拥有一个身份列的关键是我们不选择身份持有的值(在替代键上读取)。 In that case, you just remove the ID column from your insert 在这种情况下,您只需从插入内容中删除ID列

Sql("INSERT INTO Genres (Name) VALUES ('Action')");
Sql("INSERT INTO Genres (Name) VALUES ('Thriller')");
Sql("INSERT INTO Genres (Name) VALUES ('Family')");
Sql("INSERT INTO Genres (Name) VALUES ('Romance')");
Sql("INSERT INTO Genres (Name) VALUES ('Comedy')");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 {“当IDENTITY_INSERT设置为OFF时,无法在表'Cantact'中为标识列插入显式值。”} - {“Cannot insert explicit value for identity column in table 'Cantact' when IDENTITY_INSERT is set to OFF.”} “当IDENTITY_INSERT设置为OFF时,无法为表{Tags}中的标识列插入显式值。” - “Cannot insert explicit value for identity column in table {Tags} when IDENTITY_INSERT is set to OFF.” 当IDENTITY_INSERT设置为OFF时,获取“无法在表'OrderPromo'中为标识列插入显式值”。 在桌子上没有身份 - Getting 'Cannot insert explicit value for identity column in table 'OrderPromo' when IDENTITY_INSERT is set to OFF.' on table with NO Identity 当 IDENTITY_INSERT 设置为 OFF 时,无法为标识列插入显式值。 (实体框架核心) - Cannot insert explicit value for identity column when IDENTITY_INSERT is set to OFF. (Entity Framework Core) 当 IDENTITY_INSERT 设置为 OFF 时,无法在“DentalProcedures”中插入标识列的显式值。 EF代码优先 - Cannot insert explicit value for identity column in 'DentalProcedures' when IDENTITY_INSERT is set to OFF. EF code first 实体框架:当IDENTITY_INSERT设置为OFF时,无法为表'[table]'中的标识列插入显式值 - Entity Framework: Cannot insert explicit value for identity column in table '[table]' when IDENTITY_INSERT is set to OFF SqlException:当 IDENTITY_INSERT 设置为 OFF 时,无法为表 [表名] 中的标识列插入显式值 - SqlException: Cannot insert explicit value for identity column in table [Table name] when IDENTITY_INSERT is set to OFF 出现错误:当IDENTITY_INSERT设置为OFF时,无法在表'Table'中为标识列插入显式值 - Getting Error: Cannot insert explicit value for identity column in table 'Table' when IDENTITY_INSERT is set to OFF 当IDENTITY_INSERT设置为OFF时,无法为表'ActivityInstances中的标识列插入显式值 - Cannot insert explicit value for identity column in table 'ActivityInstances' when IDENTITY_INSERT is set to OFF SqlException:当IDENTITY_INSERT设置为OFF时,无法为表'Recipe'中的Identity列插入显式值 - SqlException: Cannot insert explicit value for identity column in table 'Recipe' when IDENTITY_INSERT is set to OFF
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM