Tag: ctp4

entity frameworkCTP 4.“不能将NULL值插入列” – 即使没有NULL值

我使用EF CTP 4.我有一个简单的控制台应用程序(用于testing目的),是使用EF插入一些数据到SQL数据库。 插入物品后,我遇到了问题 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这是主键,而不是一个自动递增的ID。 在创buildDataContext的时候,我有这个configuration,这个configuration是被触发的。 protected override void OnModelCreating(ModelBuilder builder) { builder.Entity<BOB>().HasKey(b => b.Id); builder.Entity<BOB>().MapSingleType().ToTable("BOB"); } 我也预先填充了这个表,然后通过debugging器能够通过监视加载这个BOB对象…所以我真的难住,至于能够加载我的BOB表明,一切都是正确的…但是插入一个新的它崩溃…

entity frameworkCTP 4 – 代码第一个自定义数据库初始化程序

我想实现一个自定义的数据库初始化策略,以便我可以生成数据库模式并使用提供的用户ID和密码将其应用到EXISTING EMPTY SQL数据库。 不幸的是内置的策略不提供我在找什么: // The default strategy creates the DB only if it doesn't exist – but it does // exist so this does nothing Database.SetInitializer(new CreateDatabaseOnlyIfNotExists<DataContext>()); // Drops and re-creates the database but then this breaks my security mapping and // only works if using a “Trusted" connection Database.SetInitializer(new RecreateDatabaseIfModelChanges<DataContext>()); // Strategy for […]