“当IDENTITY_INSERT设置为OFF时,不能在表'Movies'中为标识列插入显式值." [英] "Cannot insert explicit value for identity column in table 'Movies' when IDENTITY_INSERT is set to OFF."

查看:76
本文介绍了“当IDENTITY_INSERT设置为OFF时,不能在表'Movies'中为标识列插入显式值."的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我首先在实体框架中使用代码.我一直在收到下面的错误,无法解决该错误:

I'm using code first with entity framework. I have been getting the error below and can't figure out how to fix it:

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

"Cannot insert explicit value for identity column in table 'Movies' when IDENTITY_INSERT is set to OFF."

我已经了解到在迁移查询周围设置Sql("SET IDENTITY_INSERT Movies ON")和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.

电影表:

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

我的电影控制器:

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

我在_context.SaveChanges()上收到错误消息

I am getting the error on _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()
    {
    }
}

那是我播种dabase的唯一地方

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

推荐答案

在将记录插入流派表中时,不指定Id可以轻松避免此错误.看来Id是一个IDENTITY COLUMN.因此,当您在其中插入记录时,无需指定值.数据库将根据您定义的IDENTITY始终生成正确的Id值.通常我们有类型为INT和IDENTITY(1,1)的Columns.这意味着将要插入的第一行的值为Id为1.第二行的值为2,依此类推.

You could easily avoid this error by not specifying the Id, when you insert a record to the Genres table. As it seems Id is an IDENTITY COLUMN. 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. Usually we have Columns of type INT and IDENTITY(1,1). 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.

有关更多信息,请查看此处.

For further info please have a look here.

关于您收到IDENTITY_INSERT...的错误消息,当我们要将一个(或多个)显式值插入具有IDENTITY的列时,可以使用命令SET IDENTITY_INSERT.有关如何使用此命令的详细说明,您可以找到此处.

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. A detailed explanation on how to use this command you can find here.

这篇关于“当IDENTITY_INSERT设置为OFF时,不能在表'Movies'中为标识列插入显式值."的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆