Entity framework 实体框架如何在DB更新调用后更新实体

Entity framework 实体框架如何在DB更新调用后更新实体,entity-framework,dbcontext,Entity Framework,Dbcontext,我有客户课和订单课。 Customer类将Order类和DeliveryAddress类作为虚拟属性 class Customer { public int custId{get;set;} public string custName{get;set;} [ForiegnKey("custId")] public virtual ICollection<Order> Orders{get;set;} [ForiegnKey("custId")

我有客户课和订单课。 Customer类将Order类和DeliveryAddress类作为虚拟属性

class Customer
{
    public int custId{get;set;}
    public string custName{get;set;}
    [ForiegnKey("custId")]
    public virtual ICollection<Order> Orders{get;set;}
    [ForiegnKey("custId")]
    public virtual ICollection<DeliveryAddress> Addresses{get;set;}
}
class Order
{
    public int OrderId{get;set;}
    public int custId{get;set;}
    public string OrdDescription{get;set;}
    [ForiegnKey("custId")]
    public virtual Customer Customer{get;set;}
}
public class DomainService
{
    public void CreateAndSave()
    {
        Customer A = new Customer();
        mycontext.Entity(A).State= EntityState.Added;
        Order ord1= new Order();
        mycontext.Entity(ord1).State= EntityState.Added;
        Order ord2= new Order();
        mycontext.Entity(ord2).State= EntityState.Added;
        DeliveryAddress adr1= new DeliveryAddress();
        mycontext.Entity(adr1).State= EntityState.Added;
        DeliveryAddress adr2= new DeliveryAddress();
        mycontext.Entity(adr2).State= EntityState.Added;
        A.DeliveryAddress.Add(adr1);
        A.DeliveryAddress.Add(adr2);
        mycontext.SaveChanges();
    //After added records into Customer and Order table.I immediately call stored procedure which will update few columns in the newly added records
    }
}
class客户
{
public int custId{get;set;}
公共字符串custName{get;set;}
[ForiegnKey(“custId”)]
公共虚拟ICollection订单{get;set;}
[ForiegnKey(“custId”)]
公共虚拟ICollection地址{get;set;}
}
阶级秩序
{
公共int-OrderId{get;set;}
public int custId{get;set;}
公共字符串描述{get;set;}
[ForiegnKey(“custId”)]
公共虚拟客户客户{get;set;}
}
公共类域服务
{
public void CreateAndSave()
{
客户A=新客户();
mycontext.Entity(A).State=EntityState.Added;
订单ord1=新订单();
mycontext.Entity(ord1.State=EntityState.Added;
订单ord2=新订单();
mycontext.Entity(ord2.State=EntityState.Added;
DeliveryAddress adr1=新的DeliveryAddress();
mycontext.Entity(adr1.State=EntityState.Added;
DeliveryAddress adr2=新的DeliveryAddress();
mycontext.Entity(adr2.State=EntityState.Added;
A.交货地址。添加(adr1);
A.交货地址。添加(adr2);
mycontext.SaveChanges();
//在将记录添加到Customer和Order表中之后,我立即调用存储过程,它将更新新添加的记录中的几列
}
}
我的问题是如何更新mycontext中Customer和Order类实例的内存实例,以反映存储过程中所做的更改。 我尝试过使用重载方法,但A.Orders显示了4条记录的计数,其中2条记录来自上述代码,另外2条记录作为动态代理对象。 动态代理的另一个问题是,当我更新DeliveryAddress表中某个记录中的列时,它试图更新Order表中的所有列。
请帮助我如何在内存中重新加载和跟踪对象,以便我可以在EF

IMO中使用上下文继续进行进一步更新,最安全的方法是重新查询EF以获取新更新的数据(即使用查找方法),就像是第一次检索数据一样。我在一些文章中读到,Find方法将从.cache而不是数据库中拾取实体对象