简体   繁体   中英

Modelling a nullable foreign key Entity Framework

Take the following pseudocode database

table Foo
{
    FooID int not null,
    name varchar not null
}

table Bar
{
    BarID int not null,
    name varchar not null
}

table Waa
{
    WaaID int not null,
    name varchar not null
}

table Link
{
    LinkID int not null,
    FooID int null,
    BarID int null,
    WaaID int null,
    Description varchar not null
}

The purpose of the Link table is to allow me to link entities together in a single table, without having multiple Foo_Bar_Link , Foo_Waa_Link etc. tables.

I am moddeling this DB in EntityFramework v5. As such, i want to be able to specify a One to Many relationship between Foo and Link , so that i can get all the Links associated with a given Foo.

Problem
EntityFramework Designer only lets one-many relationships exist where the foreign key is not null. I want a Foo to have a navigation property Associations which gets all of the Association entities with FooID = Foo.FooID . As a note, i am using the Designer.

EF is not designed to support unusual models like yours. The only way which I know is to insert "empty" meaning rows for example with id equals to -1 . In business logics or in DTO implement "clever" getter and setter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM