2016-02-09 179 views
0

我在这里挣扎。我试过通过数据注释和通过Fluent API,仍然无法正常工作。现在迫切需要帮助。基本上,我有两张桌子。公司表格和地址表格。公司必须有一个总部地址(该地址应存在于地址表中),并且地址必须有一个属于该公司的地址。我真的很努力地正确设置它。实体框架1:1关系代码优先

我会把Code First Entities放到我已经得到的东西上。

[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 = "[email protected]", 
           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中的依赖属性映射到存储生成的列。列:'AddressId'。

有什么想法吗?

回答

0

你不能在SQL Server中(见How do I create a real one-to-one relationship in SQL Server)真正的一对一,但在EF一种变通方法,你做第二个实体的主键也有外键:

// [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); 
+0

感谢您的回答,我以前见过我在网上找了解决方案的拖网这一个,但它不是做安静的比赛我后。由于地址表和公司表一起使用并独立使用,因此它们都在其PK上设置了标识。其背后的想法是,一家公司只能有一个地址(这是他们的总部),但公司可能拥有多个地址(因此该地址有公司专栏)。这使我可以获得公司总部的地址以及公司拥有的所有地址。任何想法如何做到这一点? –

+0

跑出字符:).....我已经做了这个设置很好创建自己的数据库表,但似乎无法映射到EF。 :( –

+0

我们有类似的地方,一个人有很多地址,其中一个是主要地址,我们去了很多人,有3个表格Person,Address,PersonAddress。PersonAddress有导航到Person和Address以及IsPrimaryAddress标志。个人和地址每个人都有一个PersonAddresses用于导航的集合。下面是一个很好的解释:https://lostechies.com/jimmybogard/2014/03/12/avoid-many-to-many-mappings-in-orms/ –