C# 保存对RIA服务中急切加载的关联的更改

C# 保存对RIA服务中急切加载的关联的更改,c#,entity-framework-4.1,wcf-ria-services,C#,Entity Framework 4.1,Wcf Ria Services,我在Silverlight应用程序的数据层上使用RIA服务和实体框架。我有两个实体,它们在多对一关系中相互关联 public class Installation { [Key] public string Serial { get; set; } public string Description { get; set; } [Column("District")] public Guid? DistrictID { get; set; }

我在Silverlight应用程序的数据层上使用RIA服务和实体框架。我有两个实体,它们在多对一关系中相互关联


public class Installation
{
    [Key]
    public string Serial { 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; }
}
我首先为我的实体使用EF代码

以下是该服务的代码:


[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;
    }
}
加载安装时,我会包含相关的区域,这很好(我在客户端获取实体)。但是,当我更改客户端上的District并尝试更新时,实体和服务上下文上的HasChanged标志仍然为false,并且关联的外键没有更改(安装记录上的DistrictID)


有没有办法让它按照我所期望的方式工作?

结果表明,我没有正确地使用关联属性。这是必须的


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

感谢您指出这一点。

结果表明,我没有正确地使用关联属性。这是必须的


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

感谢您指出这一点。

可能由于某种原因,这些地区正在被分离。检查要保存的实体状态。实体状态未修改。也许这与我建立实体的方式有关。我将更新并添加有关实体类的更多详细信息。可能您正在修改的字段的验证失败。尝试打开服务的生成代码文件(generated_code文件夹),并在setter中为该属性设置断点。感谢提供指向生成代码的指针。结果表明,如果属性中没有IsForeignKey=true,则不会生成代码来设置实体上的外键。可能是由于某种原因,正在分离这些区域。检查要保存的实体状态。实体状态未修改。也许这与我建立实体的方式有关。我将更新并添加有关实体类的更多详细信息。可能您正在修改的字段的验证失败。尝试打开服务的生成代码文件(generated_code文件夹),并在setter中为该属性设置断点。感谢提供指向生成代码的指针。事实证明,如果属性中没有IsForeignKey=true,它不会生成代码来设置实体上的外键。