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'。
有什麼想法嗎?
感謝您的回答,我以前見過我在網上找了解決方案的拖網這一個,但它不是做安靜的比賽我後。由於地址表和公司表一起使用並獨立使用,因此它們都在其PK上設置了標識。其背後的想法是,一家公司只能有一個地址(這是他們的總部),但公司可能擁有多個地址(因此該地址有公司專欄)。這使我可以獲得公司總部的地址以及公司擁有的所有地址。任何想法如何做到這一點? –
跑出字符:).....我已經做了這個設置很好創建自己的數據庫表,但似乎無法映射到EF。 :( –
我們有類似的地方,一個人有很多地址,其中一個是主要地址,我們去了很多人,有3個表格Person,Address,PersonAddress。PersonAddress有導航到Person和Address以及IsPrimaryAddress標誌。個人和地址每個人都有一個PersonAddresses用於導航的集合。下面是一個很好的解釋:https://lostechies.com/jimmybogard/2014/03/12/avoid-many-to-many-mappings-in-orms/ –