EF4.1: Possible to have zero-or-one to zero-or-one (0..1 to 0..1) relationship?

前端 未结 3 665
轮回少年
轮回少年 2021-01-14 10:41

.NET 4.0 with SQL Server 2008 R2. I am trying to represent a 0..1 to 0..1 relationship and I keep getting the following error:

Error 113: Multiplicit

3条回答
  •  走了就别回头了
    2021-01-14 11:24

    Yes, there is a way to do what you want: make both tables subtypes of another table. This means to synchronize their PK values so that the PK value of one table is equal to a corresponding PK value in another table. If for example both tables have separate identity columns, they will both have to be changed to regular columns, the identity generated elsewhere, and then the same value used in both tables when the two tables have a one-to-one relationship.

    That is, create a supertype or "master" table that holds the PK value that will optionally be once in each of the other two tables. Make the key column of the two tables both the CLUSTERED PK of that table, and also an FK to the master table, not to the other subtype table.

    The problem is solved. The tables can both have zero or one rows of each value.

    Ultimately, if PK generation is not identity-based, and there is no data is stored in the supertype table besides the PK value, then in fact the supertype table isn't really needed, except in the case of definitively recording that the logical entity exists (it is in the supertype table) but has zero rows in both the subtype tables. If the absence of the value is enough to reliably indicate that both subtype tables have zero rows, and the PK value for the two tables can be generated without an identity column, then the supertype table can be disposed of. No FK between the two tables is actually needed, because either table is allowed to have no row matching a present row in the other table.

提交回复
热议问题