EF 4 插入与现有实体相关的实体

发布于 2024-10-06 07:52:07 字数 1273 浏览 0 评论 0原文

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();
}

替代文本 alt text

你好,我在评论中尝试所有这种情况,将我现有的操作链接到从操作继承的内部操作..但在任何这种情况下,他都会向我抛出一个错误,就像他想在数据库中插入一个新操作(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();
}

alt text
alt text

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陌上芳菲 2024-10-13 07:52:07

如果我理解正确(你试图更新关系),你可以使用存根技术

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();

If i understand you correctly (your trying to update a relationship), you can use the stub technique:

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文