简体   繁体   English

为可为空的外键实体框架建模

[英]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. Link表的目的是允许我在一个表中将实体链接在一起,而没有多个Foo_Bar_LinkFoo_Waa_Link等表。

I am moddeling this DB in EntityFramework v5. 我正在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. 因此,我希望能够指定FooLink之间的一对多关系,以便获得与给定Foo相关的所有链接。

Problem 问题
EntityFramework Designer only lets one-many relationships exist where the foreign key is not null. EntityFramework Designer仅允许在外键不为null的情况下存在一对多关系。 I want a Foo to have a navigation property Associations which gets all of the Association entities with FooID = Foo.FooID . 我希望Foo具有导航属性Associations ,该属性将获取所有具有FooID = Foo.FooIDAssociation实体。 As a note, i am using the Designer. 注意,我正在使用设计器。

EF is not designed to support unusual models like yours. EF并非旨在支持像您这样的不寻常模型。 The only way which I know is to insert "empty" meaning rows for example with id equals to -1 . 我知道的唯一方法是插入“空”的含义行,例如id为-1 In business logics or in DTO implement "clever" getter and setter. 在业务逻辑中或在DTO中,实现“聪明的” getter和setter。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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