RIA服务不更新空外键

时间:2014-01-09 17:26:52

标签: c# entity-framework silverlight code-first ria

我对RIA服务有一个奇怪的问题。我的EF CodeFirst(v4.1)上下文中有两个实体,CustomerAddress,它们之间的关系是1:1。

public class Customer
{
    [Key]
    public int Id { get; set; }

    // [...]

    [Include, Association("Customer_BillingAddress", "BillingAddressId", "Id")]
    public virtual Address BillingAddress { get; set; }
    public int? BillingAddressId { get; set; }
}

public class Address
{
    [Key]
    public int Id { get; set; }

    // [...]
}

我使用模型构建器配置它们,如下所示:

modelBuilder.Entity<Customer>()
    .HasOptional(p => p.BillingAddress)
    .WithMany()
    .HasForeignKey(x => x.BillingAddressId);

this blog article中所述。这一切都像一个魅力,但当我想在客户端这样做:

customer.BillingAddress = null;
customer.BillingAddressId = null;

RIA Services不会更新ID,导致服务器上出现外键约束错误。我检查了导航属性y和外键在保存时设置为null。因此,当我将其设置为null时,RIA Services似乎没有跟踪该属性。我该如何解决这个问题?

编辑:我完全忘了:它适用于我的开发机器,但不适用于部署。

1 个答案:

答案 0 :(得分:1)

Ghch ......找到答案here。底线:将[RoundtripOriginal]属性应用于Customer类。

相关问题