开发者

Modifying One to Many relationships - Code First

开发者 https://www.devze.com 2023-04-02 18:59 出处:网络
I thought the following task would be seemingly easy to code, but I have spent a lot of time and not yet figured out the right way to solve it. Here is the scenario

I thought the following task would be seemingly easy to code, but I have spent a lot of time and not yet figured out the right way to solve it. Here is the scenario

I have three Entities City, Locality and Donor. City has many localities and each Locality has two One to Many relationship with donor as given below.

public class City {
    public virtual List<Locality> Localities { get; set; }
}

public class Locality {
    public virtual List<Donor> DonorsInOfficeLocality { get; set; }
    public virtual List<Donor> DonorsInResidenceLocality { get; set; }
}

public class Donor : User 开发者_JAVA百科{
    public virtual Locality OfficeLocality {get;set;}
    public virtual Locality ResidenceLocality { get; set; }
}

In my Donor edit module I receive (Donor Id, residenceLocalityId and officeLocalityId). The Donor entity is in detached state comming from presentation layer. This code does not work. The old relationships remain intact.

public void EditDonor(Donor donor, int residenceLocalityId, int officeLocalityId) {
    Locality residenceLocality = context.Localities.Find(residenceLocalityId);
    donor.ResidenceLocality = residenceLocality;
    Locality officeLocality = context.Localities.Find(officeLocalityId);
    donor.OfficeLocality = officeLocality;
    context.Donors.Attach(donor);
    context.Entry(donor).State = EntityState.Modified;
    context.SaveChanges();
}

One more thing I tried is changing the state of ResidenceLocality and OfficeLocality to Modified, but that didn't work either. The old relations remained intact. And in this case, since I change the Locality as Modified, I have to additionally load City to avoid Update Exception for the modified locality.

If someone can let me know how to modify One to Many relations (as above) for entity in detached state received from presentation layer.


you can keep both navigation property and the foreign key in your model.

public class Locality {
     public int Id{get;set;}
    public virtual List<Donor> DonorsInOfficeLocality { get; set; }
    public virtual List<Donor> DonorsInResidenceLocality { get; set; }
}


public class Donor : User {
    public virtual Locality OfficeLocality {get;set;}
    public int OfficeLocalityId{get;set;}

    public virtual Locality ResidenceLocality { get; set; }
    public int ResidenceLocalityId{get;set;}
}

if you have the foreign key properties you don't need to find by id to update navigation properties,

public void EditDonor(Donor donor, int residenceLocalityId, int officeLocalityId) {
     donner.OfficeLocalityId=officeLocalityId;
     donor.ResidenceLocalityId=residenceLocalityId;
    context.Donors.Attach(donor);
    context.Entry(donor).State = EntityState.Modified;
    context.SaveChanges();
}
0

精彩评论

暂无评论...
验证码 换一张
取 消