2012-05-15 103 views
0

如何限制实体框架读取从汽车到汽车(例如)的外键关系为0..1 -> 1而不是* -> 1实体框架约束基数

我有表

create TABLE [dbo].[Vehicles](
    [Id] [int] IDENTITY(1,1) NOT NULL primary key, 
    <rest of the columns> 
) 

create TABLE [dbo].[Cars](
    [Id] [int] NOT NULL, 
    <rest of the columns> 
) 

键和外键:

alter table [Cars] add foreign key(id) references [Vehicles](id) 

,当我创建一个数据库中的模型,它是作为* -> 1创建我不能改变:

enter image description here

我知道我手动更改关联een这两个实体,但它并不重要,因为它不会改变约束

我需要这样做,将baseType属性Cars设置为Vehicle以实现继承。

目前我得到这个错误:

Multiplicity is not valid in Role 'BoatsTPT' in relationship 'FK__BoatsTPT__Id__117F9D94'. Because the Dependent Role refers to the key properties, the upper bound of the multiplicity of the Dependent Role must be 1.

+0

难道你可以将Cars的PK设置为车辆表格PK的FK,并且两者不能为NULL。 如果您在汽车表上制作车辆[Id]单独列,它应该可以工作。 – Qpirate

回答

1

,必须首先使Id列在您的Cars表的主键否则就不是一个一对一的关系。

+0

完美,我错过了。谢谢 – Diego