0

次のような、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 を読み込んだ後、Company.Locations.anything にアクセスすると、次のエラーが発生します。

Invalid column name 'Location_LocationID'.
Invalid column name 'Company_CompanyId'.

これらを手動でマッピングできることはわかっていますが、POCOの多対多の関係を作成するためのConvention Over Configuration Entity Frameworkの方法に従おうとしています。このモデルで何が間違っていましたか?

-シュナー

4

1 に答える 1