【问题标题】:How to save self tracking entities in entity framework?如何在实体框架中保存自跟踪实体?
【发布时间】:2011-10-25 05:25:12
【问题描述】:

这是我的数据库结构,我有四个表。 LocalArea 和 Lanungaue 表具有主数据,并由表 Address 和 AddressTranslated

引用

现在我想在 Address and AddressTranslated 表中添加行,我使用以下代码存储 Address 表行

Address.localarea = new localarea() { LocalAreaID = 1 };

        using (var context = new en_Entities())
        {

            context.Address.Attach(Address);
            context.ObjectStateManager.ChangeObjectState(Address.LocalArea, EntityState.Unchanged);
            context.ObjectStateManager.ChangeObjectState(Address, EntityState.Added);

            context.SaveChanges();
        }

代码工作正常并在地址表中添加行。

如何在 AddressTranslated 中添加行?我需要在 AddressTranslate 表中添加数据的更改/代码行。

【问题讨论】:

  • 白板图片+1 :)

标签: .net entity-framework self-tracking-entities


【解决方案1】:

我的 AddressTranslated 表没有任何主键列,这就是为什么不能向子表添加行的原因。

添加主键列后,所有记录都保存到数据库中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-24
    • 1970-01-01
    • 1970-01-01
    • 2011-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多