EF代码优先 - 无法插入NULL值插入列 [英] EF Code First - Cannot insert the value NULL into column

查看:166
本文介绍了EF代码优先 - 无法插入NULL值插入列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有这样的代码,第一类:

So I have this Code-First class:

[Table("Session")]
public partial class Session
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int SessionID { get; set; }

    public DateTime Start { get; set; }

    public DateTime End { get; set; }

    [StringLength(255)]
    public string Type { get; set; }

    public int PatientID { get; set; }

    public virtual Patient Patient { get; set; }
}



我用的迁移,我尝试种子会话表在我的初始化

I use Migrations, and I try to seed the Sessions table in my initializer:

IList<Session> defaultSessions = new List<Session>();
defaultSessions.Add(new Session()
{
    Start = DateTime.Parse("09/01/2014 14:00:00"),
    End = DateTime.Parse("09/01/2014 14:10:00"),
    Type = "pharyngeal",
    PatientID = 1,
});
foreach (Session session in defaultSessions)
{
    context.Sessions.Add(session);
}



的SessionID应该是主键,自动递增,这是为什么你使用 [DatabaseGenerated(DatabaseGeneratedOption.Identity)] 右键

好还是我得到这个错误:

Well still I get this error:

无法插入NULL值插入列表列不允许
空值。 INSERT失败。该语句已终止

Cannot insert the value NULL into column table column does not allow nulls. INSERT fails. The statement has been terminated

在数据库中的表如下所示(请注意我用的LocalDB,数据库是下例如 mssqllocaldb :结果
结果
我在做什么错

The table in the database looks like this (Note I'm using LocalDB, and the database is under the instance mssqllocaldb:

What am I doing wrong?

编辑:
我注意到,在数据库,身份是 FALSE ,看到这个截图:结果
在这里输入的形象描述结果
这对我奇怪,因为在最新的迁移,我做了身份的定位点(这只是我在这个问题上所提供的类),以及文件看起来是这样的:

I noted that in the database, the identity is FALSE, see this screenshot:

This strikes me weird, because in the latest migration, I did the fix on the identity (it's just the class I provided in this question), and the file looks like this:

public partial class IdentityFixes : DbMigration
{
    public override void Up()
    {
        DropPrimaryKey("dbo.Admin");
        DropPrimaryKey("dbo.Session");
        AlterColumn("dbo.Admin", "AdminID", c => c.Int(nullable: false, identity: true));
        AlterColumn("dbo.Session", "SessionID", c => c.Int(nullable: false, identity: true));
        AddPrimaryKey("dbo.Admin", "AdminID");
        AddPrimaryKey("dbo.Session", "SessionID");
    }

    public override void Down()
    {
        DropPrimaryKey("dbo.Session");
        DropPrimaryKey("dbo.Admin");
        AlterColumn("dbo.Session", "SessionID", c => c.Int(nullable: false));
        AlterColumn("dbo.Admin", "AdminID", c => c.Int(nullable: false));
        AddPrimaryKey("dbo.Session", "SessionID");
        AddPrimaryKey("dbo.Admin", "AdminID");
    }
}



正如你所看到的,标识设置为true此修复程序。我做了更新,数据库,所以我不明白为什么这不起作用。

As you can see, identity is set to true in the fix. I did Update-Database so I don't understand why this doesn't work..

推荐答案

我造成了应用迁移到现有的数据库类似的问题,所以我创建了一个空迁移,并使用SQL代码,这是我的例子:

I had a similar problem caused by applying Migrations to existing database, so I created an empty Migration and use SQL code, here is my example:

public override void Up()
{
  DropForeignKey("dbo.Bar", "FooId", "dbo.Foo");
  Sql("IF object_id(N'[dbo].[FK_Bar_Foo]', N'F') IS NOT NULL ALTER TABLE [dbo].[Bar] DROP CONSTRAINT [FK_Bar_Foo]");
  Sql("CREATE TABLE [dbo].[FooTemp] ([Id] int NOT NULL)");
  Sql("INSERT INTO [dbo].[FooTemp] ([Id]) SELECT [Id] FROM [dbo].[Foo]");
  Sql("DROP TABLE [dbo].[Foo]");
  Sql("CREATE TABLE [dbo].[Foo] ([Id] int IDENTITY (1,1) NOT NULL)");
  Sql("SET IDENTITY_INSERT [dbo].[Foo] ON");
  Sql("INSERT INTO [dbo].[Foo] ([Id]) SELECT [Id] FROM [dbo].[FooTemp]");
  Sql("SET IDENTITY_INSERT [dbo].[Foo] OFF");
  Sql("DROP TABLE [dbo].[FooTemp]");
  AddPrimaryKey("dbo.Foo", "Id");
  AddForeignKey("dbo.Bar", "FooId", "dbo.Foo", "Id");
}

public override void Down()
{
  DropForeignKey("dbo.Bar", "FooId", "dbo.Foo");
  Sql("CREATE TABLE [dbo].[FooTemp] ([Id] int NOT NULL)");
  Sql("INSERT INTO [dbo].[FooTemp] ([Id]) SELECT [Id] FROM [dbo].[Foo]");
  Sql("DROP TABLE [dbo].[Foo]");
  Sql("CREATE TABLE [dbo].[Foo] ([Id] int NOT NULL)");
  Sql("INSERT INTO [dbo].[Foo] ([Id]) SELECT [Id] FROM [dbo].[FooTemp]");
  Sql("DROP TABLE [dbo].[FooTemp]");
  AddPrimaryKey("dbo.Foo", "Id");
  AddForeignKey("dbo.Bar", "FooId", "dbo.Foo", "Id");
}



心连心

HTH

这篇关于EF代码优先 - 无法插入NULL值插入列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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