1

I have a database that contains a one-to-many relationship that is optional. I want to convert this relationship to a one-to-one required. For example one fish is required to have one tank to exist and once that fish has a tank no other fish can have that tank. But one tank can exist without one fish. I would like to preserve the data I have. My issue is when I try doing update-database; entity framework now sees that I have two id's which makes sense because the foreign key would become the primary key of the fish table. How can I work around this?

I'm using C# and SQL Server.

4

1 回答 1

0

如果它真的是一对一的关系,处理它的最简单方法是将它们存储在同一个记录中:例如

CREATE TABLE Tank
(
    tank_id    Int IDENTITY NOT NULL,
    fish_id    Int NULL
)

(没有鱼的坦克会有一个 NULL fish_id 值)

于 2013-07-08T15:51:09.730 回答