EF Code First - 无法将值NULL插入列

时间:2014-09-05 13:15:49

标签: c# sql entity-framework ef-migrations

所以我有这个Code-First类:

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

我使用迁移,并尝试在初始化程序中播种Sessions表:

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)]的原因吗?

我还是得到了这个错误:

  

无法将值NULL插入列表列不允许   空值。 INSERT失败。该声明已被终止

数据库中的表格如下所示(注意我使用的是LocalDB,数据库位于实例mssqllocaldb下面:
enter image description here
我做错了什么?

修改 我注意到在数据库中,标识为FALSE,请参见此屏幕截图:
enter image description here
这让我感到很奇怪,因为在最近的迁移中,我对身份进行了修复(它只是我在这个问题中提供的类),文件看起来像这样:

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

如您所见,修复程序中的identity设置为true。我做了Update-Database所以我不明白为什么这不起作用..

1 个答案:

答案 0 :(得分:0)

我将迁移应用到现有数据库时遇到了类似的问题,因此我创建了一个空迁移并使用SQL代码,这是我的示例:

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