如何在实体框架中保存自跟踪实体?
这是我的数据库结构,我有四个表。表 LocalArea 和 Lanungaue 具有主数据,并由表 Address 和 AddressTranslated 引用
现在我想在我使用的 Address 和 AddressTranslated 表中添加行以下代码存储地址表行
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 表中添加数据。
This is my database structure I have four tables. Table LocalArea and Lanungaue have master data and which refered by the tables Address and AddressTranslated
Now I want to add rows in Address and AddressTranslated table I used following code store Address table row
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();
}
The code working fine and adds row in Address table.
How do i add row in AddressTranslated ?? what changes/line of code do i need to add data in AddressTranslate table.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我的 AddressTranslated 表没有任何主键列,这就是无法向子表添加行的原因。
添加主键列后,所有记录都保存到数据库中。
My AddressTranslated table was not having any primary key column thats why couldnt add row to childtable.
After adding a primary key column all records got saved to DB.