实体框架1:1关系代码优先

时间:2016-02-09 17:37:31

标签: ef-code-first

我在这里挣扎。我已经尝试过数据注释和Fluent API,但仍然无法正常工作。现在急需帮助。基本上,我有两张桌子。公司表和地址表。公司必须有总公司地址(应该存在于地址表中),并且地址必须有公司也属于公司地址。我真的很难正确地设置它。

我将代码优先实体显示我已经拥有的内容。

[Table("Address")]
public class Address
{
    [Key]
    public long AddressId { get; set; }
    public string Address1 { get; set; }
    public string Address2 { get; set; }
    public string Address3 { get; set; }
    public string Address4 { get; set; }
    public string Address5 { get; set; }
    public string Town { get; set; }
    public string County { get; set; }
    public string Country { get; set; }
    public string PostCode { get; set; }
    public virtual Company Company { get; set; }
    public DateTime? RemovedDate { get; set; }
    public long? RemovedBy { get; set; }
}

[Table("Company")]
public class Company
{
    [Key ]
    public long CompanyId { get; set; }
    public string Name { get; set; }
    public string WebsiteUrl { get; set; }
    public virtual Address Address { get; set; }
    public User LeadUser { get; set; }
    public DateTime ActiveSince { get; set; }
    public DateTime? ActiveTill { get; set; }
    public string VatRegistration { get; set; }
    public string LicenseKey { get; set; }
    public LicenseStatus LicenseStatus { get; set; }
    public bool CanAgreementBeExtended { get; set; }
    public string BillingEmail { get; set; }
    public string PhoneNumber { get; set; }
    public string MobileNumber { get; set; }
    public DateTime DateCreated { get; set; }
    public DateTime DateUpdated { get; set; }
    public virtual ICollection<User> Users { get; set; }
    public virtual ICollection<LicenseHistory> LicenseHistories { get; set; }
}

//Seeded data inserted as follows
 var testCompany = new Company
                          {
                              ActiveSince = DateTime.UtcNow,
                              Name = "Test Company",
                              LeadUser = adminUser,
                              DateCreated = DateTime.UtcNow,
                              DateUpdated = DateTime.UtcNow,
                              BillingEmail = "admin@test.co.uk",
                              CanAgreementBeExtended = true,
                              LicenseStatus = LicenseStatus.PendingAgreement,
                              MobileNumber = "1234567890",
                              PhoneNumber = "1234567890",
                              VatRegistration = "1234567890"
                          };

        context.Companies.AddOrUpdate(u => u.Name, testCompany);

var testAddress = new Address
        {
            Address1 = "Test Ltd",
            Address2 = "1 Test Gardens",
            Address3 = "Test Heath",
            Address4 = string.Empty,
            Address5 = string.Empty,
            County = "Test",
            Town = "Test",
            Country = "United Kingdom",
            PostCode = "TE5 T11",
            Company = testCompany
        };

        context.Addresses.AddOrUpdate(u => new { u.AddressId }, testAddress);

        testCompany.Address = testAddress;

        context.Companies.AddOrUpdate(u => u.Name, testCompany);

//Fluent API set up as follows in the OnModelCreating
            modelBuilder.Entity<Address>()
                    .HasRequired(ad => ad.Company)
                    .WithOptional(s => s.Address);

有人能发现我做错了什么吗?在过去的几天里,我一直在玩不同的组合,但它不起作用。我只是不断收到错误,基于上面代码的最新错误是......

  

ReferentialConstraint中的依赖属性映射到存储生成的列。专栏:&#39; AddressId&#39;。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

您无法在SQL Server中实现真正的一对一(请参阅How do I create a real one-to-one relationship in SQL Server),但在EF中有一种解决方法,您可以将第二个实体的主键也设为外键: / p>

// [Table("Company")] -- not needed unless different
public class Company
{
    // [Key ] -- will be key by convention
    public long CompanyId { get; set; }
    ...
    public virtual Address Address { get; set; }
}

public class Address
{
    [Key, ForeignKey("Company")]
    public long AddressId { get; set; }
    public string Address1 { get; set; }
    ...
    public virtual Company Company { get; set; }
}

您也可以使用流利的代码执行此操作:

modelBuilder.Entity<Company>() 
    .HasRequired(t => t.Address) 
    .WithRequiredPrincipal(t => t.Company);