0
我公司所在地的一個簡單的表結構和許多一對多CompanyLocation表,像這樣:C#POCO許多一對多的關係,越來越Tablename_Fieldname
create table Company
(
CompanyId bigint not null identity,
CompanyName varchar(50),
--other fields
constraint PK_Company primary key (CompanyId)
)
create table Location
(
LocationId int not null identity,
LocationName varchar(50),
--other fields
constraint PK_Location primary key (LocationId)
)
create table CompanyLocation
(
CompanyId bigint not null,
LocationId int not null,
constraint PK_CompanyLocation primary key (CompanyId, LocationId),
constraint FK_CompanyLocation_Company foreign key (CompanyId) references Company(CompanyId),
constraint FK_CompanyLocation_Location foreign key (LocationId) references Location(LocationId)
)
所以我多對多表是一個「正確的」唯一鍵表。我的POCO類被定義爲這樣的:
public class Company
{
public long CompanyId { get; set; }
public string CompanyName { get; set; }
//more fields
public virtual List<Location> Locations { get; set; }
}
public class Location
{
public int LocationId { get; set; }
public string LocationName { get; set; }
//more fields
public virtual List<Company> Companies { get; set; }
}
它編譯發現,當我運行它,在我加載一個公司,如果我訪問Company.Locations.anything,我得到以下錯誤:
Invalid column name 'Location_LocationID'.
Invalid column name 'Company_CompanyId'.
我知道我可以手動映射這些,但我正在嘗試遵循Convention Over Configuration Entity Framework創建POCO多對多關係的方式。我對這個模型做了什麼錯誤?
-shnar
好了,我的表有外鍵彼此,我以爲EF只是應該「知道」,併爲我們做這種映射,按照慣例?我通過手動強制並在位置列表上方放置了[ForeignKey(「LocationId」)]屬性來實現這一目標,但是我認爲如果我們遵循EF的約定,我們不必這麼做? – shnar 2013-08-22 23:28:04