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

标签 c# sql entity-framework entity-framework-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; }
}

我使用迁移,并尝试在我的初始化程序中为 session 表设置种子:

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

我仍然收到这个错误:

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

数据库中的表是这样的(注意我用的是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 所以我不明白为什么这不起作用..

最佳答案

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

相关文章:

c# - autofac 中的工厂模式

mysql - MySQL 中的数据透视表 - 转换为具有 varchar 格式值的数据透视表

sql - 使用 SELECT 结果作为其他 SELECT 中的 COLUMN 名称

c# - EF 迁移和时间戳列,无法更新/运行

c# - 破解你自己的应用程序

c# - Flowlayout 面板在调整大小后不显示滚动条

c# - Windows:如何将路径很长的文件移动到回收站?

java - 错误 : Make sure the Cursor is initialized correctly before accessing data from it?

c# - 在 EF 的 DbContext.SaveChangesAsync() 中引发异步事件

c# - EF过滤/搜索多个词