开发者

Saving changes to eagerly loaded associations in RIA Services

开发者 https://www.devze.com 2023-04-10 01:59 出处:网络
I\'m using RIA Services with Entity Framework for the data layer of my Silverlight application.I have two entities that are related in a many-to-one relationship

I'm using RIA Services with Entity Framework for the data layer of my Silverlight application. I have two entities that are related in a many-to-one relationship


public class Installation
{
    [Key]
    public string S开发者_运维知识库erial { get; set; }
    public string Description { get; set; }
    [Column("District")]
    public Guid? DistrictID { get; set; }

    [Include]
    [Association("InstallationDistrict", "DistrictID", "DistrictID")]
    public District District { get; set; }
}

public partial class District
{
    [Key]
    public Guid DistrictID { get; set; }
    public string DisplayName { get; set; }
}

I am using EF Code First for my entities.

Here is the code for the service:


[EnableClientAccess]
public class EagerLoadingService : DomainService
{
    private readonly CentralContext _context;
    public EagerLoadingService()
    {
        _context = new CentralContext();
    }
    [Query]
    public IQueryable GetInstallations()
    {
        return _context.Installations.Include("District");
    }
    [Update]
    public void UpdateInstallation(Installation i)
    {
        _context.Installations.Find(i.Serial).District = i.District;
        _context.SaveChanges();
    }
    [Query]
    public IQueryable GetDistricts()
    {
        return _context.Districts;
    }
}

When loading an Installation, I include the associated District, which works fine (I'm getting the entity on the client side). However, when I change the District on the client and try to update, the HasChanged flag is still false on the entity and the service context, and the associated foreign key doesn't change (DistrictID on the Installation record).

Is there a way to get this to work the way I'm expecting it to?


Turns out I was doing the Association attribute incorrectly. It needed to be


[Association("InstallationDistrict", "DistrictID", "DistrictID", IsForeignKey = true)]

Thanks to this guide for pointing it out.

0

精彩评论

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