开发者

How to map a 0..1 to 1 relationship in Entity Framework 3

I have two tables, Users, and Address. A the user table has a field that maps to the primary key of the address table. 开发者_JS百科This field can be null.

In plain english, Address exist independent of other objects. A user may be associated with one address. In the database, I have this set up as a foreign key relationship.

I am attempting to map this relationship in the Entity Framework. I am getting errors on the following code:

<Association Name="fk_UserAddress">
      <End Role="User" Type="GenesisEntityModel.Store.User" Multiplicity="1"/>
      <End Role="Address" Type="GenesisEntityModel.Store.Address" Multiplicity="0..1" />
      <ReferentialConstraint>
        <Principal Role="Address">
          <PropertyRef Name="addressId"/>
        </Principal>
        <Dependent Role="User">
          <PropertyRef Name="addressId"/>
        </Dependent>
      </ReferentialConstraint>
    </Association>

It is giving a "The Lower Bound of the multiplicity must be 0" error.

I would appreciate it if anyone could explain the error, and the best way to solve it.

Thanks for any help.


The reason for the error is because the table with the foreign key (User) potentially has a multiplicity greater than the primary (Address) table. The database relationship you describe in the first paragraph is 1 Address - to 0 or many Users. The EF relationship can't have zero as an option for the Primary table.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜