【发布时间】:2014-11-05 12:47:47
【问题描述】:
有没有办法让 ExecuteSqlCommand 与新的未提交实体一起工作。
using (var context = new EarthContext())
{
var country = new Country(){
Id = "ZZZ",
IsoCodeAlpha2 = "ZZ",
IsoCodeNumberic = 999
};
context.Countries.Add(country);
context.Database.ExecuteSqlCommand(
@"
INSERT INTO dbo.Location([Line1],[CountryId])
VALUES ('random line','ZZZ')
");
context.SaveChanges();
}
它给出了“与 FOREIGN KEY 约束冲突的 INSERT 语句”异常,因为 ExecuteSqlCommand 在新实体提交之前执行。
*代码必须在一个事务中运行,即我不能在 ExecuteSqlCommand 之前提交更改
【问题讨论】: