Dummy
Dummy

Reputation: 265

Change edmx designer, manually change the multiplicity of the "1" side of the association so it becomes 1-n

I'm using database first approach and I'm getting this error

Severity Code Description Project File Line Suppression State Error Error 113: Multiplicity conflicts with the referential constraint in Role 'UserInformation' in relationship 'FK_Tokens_UserInformation'. Because all of the properties in the Dependent Role are non-nullable, multiplicity of the Principal Role must be '1'. DataModel F:\kode\TheWork\TheWork2\TheWork\DataModel\Model1.edmx 134

Some one said I can manually change in the edmx designer, manually change the multiplicity of the "1" side of the association so it becomes 1-n

But How I tried to do that but there is no option available

Update enter image description here

Upvotes: 0

Views: 539

Answers (1)

Karel Tamayo
Karel Tamayo

Reputation: 3750

Severity Code Description Project File Line Suppression State Error Error 113: Multiplicity conflicts with the referential constraint in Role 'UserInformation' in relationship 'FK_Tokens_UserInformation'. Because all of the properties in the Dependent Role are non-nullable, multiplicity of the Principal Role must be '1'. DataModel F:\kode\TheWork\TheWork2\TheWork\DataModel\Model1.edmx 134

As per the error message you should choose the second option in the image you provide. That will set the multiplicity for the relation to 1 (strict).

There is an important difference here between both options. In the first case, you have 0..1 (Zero or One) and that only will be possible to manage if your foreign key field on the dependent entity is of a nullable type.

Lets say you have:

public class UserInformation
{
    public ICollection<Token> Tokens {get;set;}
}

public class Token
{
    public int UserInformationId {get;set;}

    public UserInformation UserInfo {get;set;}
}

As you can see, there is no way with this design to handle the case for when you don't have any UserInfo linked to a given Token, because the field UserInformationId is of type int, hence it'll always have an acceptable value. It turns out to be 0 by default, but that's still a valid value for a FK.

So, EF is letting you know that due to the properties in the model you need to set the multiplicity to strict 1. If you want to be able to represent the relation as 0..1 the you need to allow the FK to be Nullable<int> for the case when no UserInformation is present.

Hope this helps!

Upvotes: 1

Related Questions