Reputation: 10193
I have put a new table in my database and there are 4 tables that have a foreign key relationship with it. I thought I configured all 4 in the same way, but I get this error;
Error 15 Error 113: Multiplicity is not valid in Role 'ReportCellImage' in relationship 'FK_OtherLeaves_ReportCellImages'. Because all the properties in the Dependent Role are nullable, multiplicity of the Principal Role must be '0..1'.
So to simplify my tables;
ReportCellImage table contains the ReportCellImageId field which in the primary key and an integer
OtherLeave table contains the ReportCellImageId field as a nullable foreign key with a default of 4
The other tables are similar and all have a foreign key configured to join with the ReportCellImage table
This is a recent change, so when I update my edmx file from the database, why do I get this error and how do I fix it?
Upvotes: 72
Views: 88699
Reputation: 61
It is asking you to change the Principal Role to 0..1 not the entire relationship so that the relation ship becomes 0-1 to Many.
Upvotes: 5
Reputation: 21
This error is mainly because the table definitions might have changed after adding to the EF. The solution is to delete it from EF Model and add again using update model from database.
Upvotes: 2
Reputation: 655
Change Multiplicity="1" to Multiplicity="0..1"
<Association Name="FK_O_Personel_PBS_AtanmaSekilleri">
<End Type="eKampus_RTEUModel.PBS_AtanmaSekilleri" Role="PBS_AtanmaSekilleri" Multiplicity="0..1" />
<End Type="eKampus_RTEUModel.O_Personel" Role="O_Personel" Multiplicity="*" />
<ReferentialConstraint>
<Principal Role="PBS_AtanmaSekilleri">
<PropertyRef Name="ID" />
</Principal>
<Dependent Role="O_Personel">
<PropertyRef Name="atamaSekliID" />
</Dependent>
</ReferentialConstraint>
</Association>
Upvotes: 1
Reputation: 653
I'm using a database first approach to create the .edmx file. When I ran into this issue, I tried a couple of the suggestions above, but was still getting errors, so I deleted the .edmx file and refreshed the entire file from the updated database.
I've found that sometimes EF gets confused after updates to the existing database, and while it's ideal to find the root cause, spending an hour working on something may not be possible in a work environment (such as mine)
Our DB is new and small so refreshing the file is easy and doesn't take that long.
Problem went away, moving on . . .
Upvotes: 5
Reputation: 440
I deleted the updated table from the model and then in Update Model from Database, added it again and it helped.
Upvotes: 7
Reputation: 101
After removing the Entity reference in the entity classes then removing the reference manually in the database, then putting them back step by step to resolve the issue I realized after I finished the quick solution would have been to replace the WithOptional with WithRequired in the Context after I first changed the field from nullable to non nullable.
protected override void OnModelCreating(DbModelBuilder modelBuilder)
Upvotes: 0
Reputation: 31
I had the same problem after adding a new table, I tried all the solutions here including dropping the relationship, but that didn't work. The solution that worked for me was to actually delete the model completely and create a new model. Don't forget to delete the connection string in the web.config so that your new EF will be able to use the same name.
Upvotes: 2
Reputation: 594
Right click on the relationship in your EDX diagrame. In proprety, change END1 Multiplicity to 0..1 (Zero or One of YOURTABLENAME)
Upvotes: 17
Reputation: 11040
I just had the same message and it was perplexing because the tables I modified in the DB were different from the ones I was getting the message for.
I tried changing the multiplicity 0..1-to-many but the message persisted, even after "Run Custom Tool" commands, cleans and rebuilds.
Resolved by dropping the relationship EF was complaining about and updating the model from DB
Upvotes: 72
Reputation: 364249
If your FK is nullable your multiplicity in principal entity must be 0..1 - default value has no role in this because you can assign null to FK. So all your dependent entities must be in 0..1 - * relation with your principal entity.
Upvotes: 62