所以我有这个代码优先类:

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

我使用 Migrations,并尝试在我的初始化程序中为 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)] 的原因,对吗?

好吧,我仍然收到此错误:



数据库中的表如下所示(注意我使用的是 LocalDB,并且数据库在实例 mssqllocaldb 下:

我究竟做错了什么?

编辑:
我注意到在数据库中,身份是 FALSE ,看这个截图:

这让我觉得很奇怪,因为在最新的迁移中,我修复了身份(这只是我在这个问题中提供的类),文件看起来像这样:
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。我做了 Update-Database 所以我不明白为什么这不起作用..

最佳答案

我在将迁移应用于现有数据库时遇到了类似的问题,因此我创建了一个空迁移并使用 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

关于c# - EF Code First - 无法将值 NULL 插入列,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25686804/

10-13 03:04