using (EntitiesContainer db = new EntitiesContainer())
{
Language language = db.Languages.SingleOrDefault(x => x.Culture == _session.Language);
Language language2 = new Language { Id = action.Language.Id };
Operation operation = new Operation { Id = action.Operation.Id };
//Operation operation = db.Operations.SingleOrDefault(x => x.Id == action.Operation.Id);
if (!language.Id.Equals(language2.Id))
{
db.Languages.Attach(language2);
action.Language = language2;
}
else
{
action.Language = language;
}
db.Operations.Attach(operation);
//db.ObjectStateManager.ChangeObjectState(operation, System.Data.EntityState.Unchanged);
action.Operation = operation;
//operation.Internals.Add(action);
action.CurrentDetail.Language = language;
action.CurrentDetail.Id = Guid.NewGuid();
action.Id = Guid.NewGuid();
db.SaveChanges();
}
你好我在评论中尝试所有这个场景,将我现有的操作链接到从操作继承的内部操作..但在任何一种情况下,他都会给我一个错误,就像他想在数据库中插入一个新操作一样(dbo.Operations可以接受“Action”NULL值)但实体已经存在..有人可以请给我一个黄金法则..插入有关系的实体..在EF中是否存在。这让我发疯了!
Cordialy, 于连。
答案 0 :(得分:0)
如果我理解正确(您尝试更新关系),则可以使用存根技术:
Language lang = new Language { Id = action.language_id }; // create stub with ID
db.Languages.Attach(lang); // attach stub to graph
action.language = lang; // update relationship
db.SaveChanges();