1

我正在使用 Effort (for EF4) 进行一些单元测试。

var ctx= Effort.ObjectContextFactory.CreateTransient<TheContext>(Shared.Connection);
ctx.companies.AddObject(new company() { ID = 100, name = "Agent", is_agent = true });
ctx.SaveChanges(System.Data.Objects.SaveOptions.DetectChangesBeforeSave);

公司中的 ID 列是一个身份字段。执行上述查询后,发现ID值是1而不是100。在使用Effort时有什么方法可以控制Identity_insert

4

1 回答 1

2

这只是在此线程上添加解决方案。@MrBlueSky 在上面的评论中添加解决方案的归档链接。但是,DbConfiguration在链接上使用的,在最新的 Effort 中不再存在。解决方案仍然存在于Effort.EF6 [version="1.3.0"]. 并且可以使用 on 方法SetIdentityFields打开EffortConnection.DbManager或关闭标识字段

if (Effort.DbConnectionFactory.CreateTransient() is EffortConnection connection)
{
    connection.Open();
    connection.DbManager.SetIdentityFields(false);
    connection.DbManager.ClearMigrationHistory();
    connection.Close();
}

开启

// Add data with explicitly set id
Person initPerson = new Person { Id = 5, FirstName = "John", LastName = "Doe" };
dataInitContext.People.Add(initPerson);
dataInitContext.SaveChanges();

Assert.AreEqual(5, initPerson.Id);

// Enable identity field
connection.Open();
connection.DbManager.SetIdentityFields(true);
connection.Close();
于 2018-03-28T09:37:59.757 回答