EF 4 插入与现有实体相关的实体
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 中的实体。这让我发疯!
诚挚地, 朱利安.
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();
}
Hello I Try all this scenario in commentary, for link my existing operation to the internal action that inherited from action .. but in any of this scenario, he throw me an error like he want to insert a new operation in the DB (dbo.Operations can accept "Action" NULL value) but the Entity already exist.. Can someone please, give me the golden rule .. to insert entity with relation .. existing or not in EF. It's driving me crazy!
Cordialy,
Julien.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我理解正确(你试图更新关系),你可以使用存根技术:
If i understand you correctly (your trying to update a relationship), you can use the stub technique: