实体框架CTP 4"不能插入NULL值插入列" - 即使是没有空值 [英] Entity Framework CTP 4. "Cannot insert the value NULL into column" - Even though there is no NULL value

查看:129
本文介绍了实体框架CTP 4"不能插入NULL值插入列" - 即使是没有空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

林EF CTP 4.我有一个简单的控制台应用程序(用于测试目的)使用EF一些数据插入到SQL数据库。

Im using EF CTP 4. I have a simple console app (for testing purposes) that is using EF to insert some data into a SQL database.

我在插入的项目来一个问题,即通过

I have come to a problem where by upon inserting the item

using(var context = GetContext())
{
   BOB b = new BOB();
   b.Id = 1;

   context.Bobs.Add(b);
   context.SaveChanges();
}

这引发错误: {无法插入NULL值插入列'ID',表'TestDB.dbo.BOB';列不允许空INSERT失败\\ r \\ n此语句已终止。 }

表只是拥有的 1场ID INT NOT NULL 的是主键,是的不可以自动递增的编号。

The Table just has 1 field of Id int NOT NULL which is the primary key and is not an auto incremented Id.

在创建的DataContext我有这样的配置,这是不会被解雇的。

On the creation of the DataContext I have this configuration, which yes does get fired.

protected override void OnModelCreating(ModelBuilder builder)
{
    builder.Entity<BOB>().HasKey(b => b.Id);
    builder.Entity<BOB>().MapSingleType().ToTable("BOB");
}

我也pre-填充此表,然后通过调试通过能的的加载这个BOB对象......所以我真的很为难,作为能够加载了我的BOB表明,所有是正确的......但是在插入一个新的崩溃...

I have also pre-populated this table and then through the debugger been able to via watch load up this BOB object... so I am really stumped, as for being able to load up my BOB shows that all is right... however upon inserting a new one it crashes...

推荐答案

您是否尝试过明确指定 StoreGeneratedPattern

Have you tried explicitly specifying the StoreGeneratedPattern?

modelBuilder.Entity<BOB>()
    .HasKey(p => p.Id)
        .Property(p => p.Id)
            .StoreGeneratedPattern = StoreGeneratedPattern.None;

builder.Entity<BOB>().MapSingleType().ToTable("BOB");

这篇关于实体框架CTP 4&QUOT;不能插入NULL值插入列&QUOT; - 即使是没有空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆