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 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. 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??

Edited: I am using Entity Framework 6 DB First with Oracle 11i as the back end.


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.

链接地址: http://www.djcxy.com/p/5740.html

上一篇: 惊讶的是实体框架正确保存分离的实体

下一篇: 如何更新实体框架中的相关数据6