EF代码优先 - 无法将值NULL插入列 [英] EF Code First - Cannot insert the value NULL into column
问题描述
所以我有这个Code-First类:
[Table(Session)]
public partial class Session
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int SessionID {get;组; }
public DateTime Start {get;组; }
public DateTime End {get;组;
[StringLength(255)]
public string Type {get;组; }
public int PatientID {get;组; }
public virtual患者病人{get;组;
}
我使用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(默认会话中的会话会话)
{
context.Sessions.Add(session);
}
SessionID应该是主键和自动递增,这是为什么你使用 [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
对?
还是我得到这个错误: p>
无法将值NULL插入列表列不允许
nulls。 INSERT失败。该语句已被终止
数据库中的表格如下所示(注意我使用的是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修复。我做了更新数据库
,所以我不明白为什么这不工作..
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
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);
}
The SessionID is supposed to be the Primary Key, and Auto-Incremented, that's why you use [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
right?
Well still I get this error:
Cannot insert the value NULL into column table column does not allow nulls. INSERT fails. The statement has been terminated
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?
EDIT:
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");
}
}
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..
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屋!