[英]How to update related data in Entity Framework 6
I have an Entity Territory 我有一个实体领地
public partial class Territory
{
public Territory()
{
this.Employees = new HashSet<Employee>();
}
public string TerritoryID { get; set; }
public string TerritoryDescription { get; set; }
public int RegionID { get; set; }
public virtual Region Region { get; set; }
public virtual ICollection<Employee> Employees { get; set; }
}
And I have an Entity region 我有一个实体区域
public partial class Region
{
public Region()
{
this.Territories = new HashSet<Territory>();
}
public int RegionID { get; set; }
public string RegionDescription { get; set; }
public virtual ICollection<Territory> Territories { get; set; }
}
On my Client application I make objects of Territory class and Region class to insert and update data in database and pass those objects to my AddTerriory and UpdateTerriory method in my repository class. 在我的客户端应用程序上,我创建Territory类和Region类的对象以插入和更新数据库中的数据,并将这些对象传递给我的存储库类中的AddTerriory和UpdateTerriory方法。
Territory obj = new Territory();
obj.TerritoryID = "12123";
obj.TerritoryDescription = "test terriotry 1";
obj.RegionID = 9;
obj.Region = new Region{RegionDescription = "test region 1"};
public void UpdateTerriory(Territory territory)
{
using (var context = new travelEntities())
{
context.Territories.Attach(territory);
context.Entry(territory).State = EntityState.Modified;
context.SaveChanges();
}
}
}
public static void AddTerriory(Territory territory)
{
using (var context = new travelEntities())
{
context.Territories.Add(territory);
context.SaveChanges();
}
}
But its not working and its throwing errors. 但是它不起作用并且抛出错误。 How should I build my objects when I have to update and insert Territory and Region.
当我必须更新并插入Territory和Region时,应如何构建对象。 At times some fields of Region are also updated when updating Territory, so I have to update that too.
有时,在更新“地区”时也会更新“区域”的某些字段,因此我也必须进行更新。 And when we add a new Territory then we add a new region too.
当我们添加一个新的地区时,我们也添加了一个新的地区。 We don't use existing Region when adding a new Territory.
添加新的地区时,我们不使用现有的地区。
How should I build my objects when I have to update and insert Territory and Region?? 当我必须更新并插入Territory和Region时应如何构建对象?
Edited: I am using Entity Framework 6 DB First with Oracle 11i as the back end. 编辑:我正在使用以Oracle 11i为后端的Entity Framework 6 DB First。
First off, why would you use string representation of territory identifier? 首先,为什么要使用地区标识符的字符串表示形式? Can it be integer?
可以是整数吗?
public void InsertOrUpdate(Territory territory)
{
using (var context = new TravelEntities())
{
context.Entry(territory).State =
string.IsNullOrWhiteSpace(territory.TerritoryID) ?
EntityState.Added :
EntityState.Modified;
if (territory.Region!=null)
{
context.Entry(territory.Region).State =
territory.Region.RegionID == 0 ?
EntityState.Added :
EntityState.Modified;
}
context.SaveChanges();
}
}
you can make this method as a generic one, taking entity and key selector function as parameters. 您可以将该方法作为通用方法,以实体和键选择器功能为参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.