【发布时间】:2011-09-29 22:03:05
【问题描述】:
我使用实体框架 4.1、SQL Server 2008 和 .NET 4.0
我有一个 Sql 表:
create table SchemaVersion (
Version int not null,
constraint PK_SCHEMAVERSION primary key (Version)
)
还有 POCO
[Table("SchemaVersion")]
public class SchemaVersion
{
[Key]
public Int32 Version { get; set; }
}
我在表格中添加了列
Context ctx = new Context();
ctx.SchemaVersions.Add(new SchemaVersion() { Version = 2 });
ctx.SaveChanges();
在我得到异常之后:
无法将值 NULL 插入“版本”列,表 'Simply.dbo.SchemaVersion';列不允许空值。插入失败。 声明已终止。
上下文设置:
public class Context : DbContext
{
public DbSet<SchemaVersion> SchemaVersions { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
this.Configuration.ValidateOnSaveEnabled = false;
base.OnModelCreating(modelBuilder);
}
}
我觉得很奇怪,谁能帮我解决这个问题
【问题讨论】:
-
你能展示一下你的上下文是如何设置的代码吗?
标签: .net sql-server-2008 null entity-framework-4.1