保存对 RIA 服务中急切加载的关联的更改
我将 RIA 服务与实体框架一起用于 Silverlight 应用程序的数据层。我有两个以多对一关系相关的实体,
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 Code First。
以下是该服务的代码:
[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;
}
}
加载安装时,我包含了关联的区域,该区域工作正常(我在客户端获取实体)。但是,当我更改客户端上的区并尝试更新时,实体和服务上下文上的 HasChanged 标志仍然为 false,并且关联的外键不会更改(安装记录上的 DistrictID)。
有没有办法让它按照我期望的方式工作?
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 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; }
}
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
事实证明我错误地使用了关联属性。它需要感谢
本指南指出了这一点。
Turns out I was doing the Association attribute incorrectly. It needed to be
Thanks to this guide for pointing it out.