我有一个简单的 Company、Location 表结构和一个多对多 CompanyLocation 表,如下所示:
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; }
}
它编译 find,当我运行它时,在我加载公司后,如果我访问 Company.Locations.anything,我会收到以下错误:
Invalid column name 'Location_LocationID'.
Invalid column name 'Company_CompanyId'.
我知道我可以手动映射这些,但我正在尝试遵循配置实体框架的约定来创建 POCO 多对多关系。我对这个模型做错了什么?
-shnar