'System.Data.Entity.Infrastructure.DbUpdateException'EntityFramework

时间:2017-12-06 23:05:04

标签: c# mysql asp.net asp.net-mvc entity-framework

SqlException当IDENTITY_INSERT设置为OFF时,无法在表'电影'中为标识列插入显式值

我正在将电影添加到电影表但是跟随错误累积在_context.SaveChanges(); SqlException:当IDENTITY_INSERT设置为OFF时,无法在表'电影'中插入标识列的显式值吗?

Table Movie属性Id在创建时不是标识,我手动添加了一些数据,后来在我创建Id Identity时删除了Data,

早些时候当Id不是身份第一条记录总是存储在0 Id时显而易见但是当我通过电影表定义或查询创建Id Identity之后它甚至不存储单个记录并给出此异常

。我通过Code 1st Migration创建了数据库。表定义:

CREATE    TABLE    [dbo].[Movies]    (
[Id]          INT            IDENTITY    (1, 1)    NOT    NULL   ,
[Name]        NVARCHAR    (255)    NOT    NULL   ,
[GenreId]      TINYINT        NOT    NULL   ,
[DateAdded]      DATETIME       NOT    NULL,
[ReleaseDate]    DATETIME       NOT    NULL,
[Stock]       TINYINT        NOT    NULL,
CONSTRAINT    [PK_dbo.Movies]    PRIMARY    KEY    CLUSTERED    ([Id] ASC),
CONSTRAINT    [FK_dbo.Movies_dbo.Genres_GenreId]    FOREIGN    KEY    ([GenreId])    REFERENCES    [dbo].[Genres]    ([Id])    ON    DELETE    CASCADE); 

GO    CREATE     NONCLUSTERED INDEX [IX_GenreId]
ON [dbo].[Movies]([GenreId] ASC);

电影课程:

public class Movie
{
    public  byte Id { get; set; } 
    [Required]
    [StringLength(255)]
    public string Name { get; set; }
    public Genre Genre { get; set; }
    public byte GenreId { get; set; }
    public DateTime ReleaseDate { get; set; }
    public DateTime DateAdded { get; set; }
    public short Stock { get; set; }
}

MovieForm:

  @using (@Html.BeginForm("Save","Movies")){ 
<div class="form-group">
    @Html.LabelFor(m => m.Movie.Name, new { @class = "control-label" })
    @Html.TextBoxFor(m => m.Movie.Name, new { @class = "form-control" })
</div>
<div class="form-group">
    @Html.LabelFor(m => m.Movie.ReleaseDate,  new { @class = "control-label" })
    @Html.TextBoxFor(m => m.Movie.ReleaseDate, new { @class = "form-control" })
</div>
<div class="form-group">
    @Html.LabelFor(m => m.Genres, new { @class = "control-label" })
    @Html.DropDownListFor(m => m.Movie.GenreId, new SelectList(Model.Genres, "Id", "Name"), "Select Genre", new { @class = "form-control" })
</div>
<div class="form-group">
    @Html.LabelFor(m => m.Movie.Stock, new { @class = "control-label" })
    @Html.TextBoxFor(m => m.Movie.Stock, new { @class = "form-control" })
</div>
@Html.HiddenFor(m => m.Movie.Id);
<button type="submit" class="btn btn-primary">Submit</button>}

保存功能:

public ActionResult Save(Movie movie)
    {
        if (movie.Id == 0) 
        {
            movie.DateAdded = DateTime.Now;
            _context.Movies.Add(movie);
        }
        else
        {
            var movieInDb = _context.Movies.Single(m => m.Id == movie.Id);
            movieInDb.Name = movie.Name;
            movieInDb.GenreId = movie.GenreId;
            movieInDb.ReleaseDate = movie.ReleaseDate;
            movieInDb.Stock = movie.Stock;
        }
        try
        {
            _context.SaveChanges();
        }
        catch (DbEntityValidationException e)
        {

            Console.WriteLine(e);
        }

        return RedirectToAction("Index", "Movies");
    }

将Movie类添加到Identity DbContext时的迁移:

public override void Up()
    {
        CreateTable(
            "dbo.Movies",
            c => new
            {
                Id = c.Int(nullable: false, identity: true),
                Name = c.String(nullable: false, maxLength: 255),
                GenreId = c.Byte(nullable: false),
                DateAdded = c.DateTime(nullable: false),
                ReleaseDate = c.DateTime(nullable: false),
                Stock = c.Byte(nullable: false),
            })
            .PrimaryKey(t => t.Id)
            .ForeignKey("dbo.Genres", t => t.GenreId, cascadeDelete: true)
            .Index(t => t.GenreId);

        CreateTable(
            "dbo.Genres",
            c => new
            {
                Id = c.Byte(nullable: false),
                Name = c.String(nullable: false, maxLength: 255),
            })
            .PrimaryKey(t => t.Id);

    }

    public override void Down()
    {
        DropForeignKey("dbo.Movies", "GenreId", "dbo.Genres");
        DropIndex("dbo.Movies", new[] { "GenreId" });
        DropTable("dbo.Genres");
        DropTable("dbo.Movies");
    }

是因为已经存在的数据库???

0 个答案:

没有答案