Entity Framework 4.0: Error 113: Multiplicity is not valid in Role

Entity Framework

Entity Framework Problem Overview


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?

Entity Framework Solutions


Solution 1 - Entity Framework

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

Solution 2 - Entity Framework

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.

Solution 3 - Entity Framework

Right click on the relationship in your EDX diagrame. In proprety, change END1 Multiplicity to 0..1 (Zero or One of YOURTABLENAME)

Solution 4 - Entity Framework

I deleted the updated table from the model and then in Update Model from Database, added it again and it helped.

Solution 5 - Entity Framework

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 . . .

Solution 6 - Entity Framework

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.

  1. Right click on the relationship line in the edmx graph
  2. Select properties
  3. Change End1Multiplicity (ReportCellImages) from 1 to 0..1
  4. Rebuild

Solution 7 - Entity Framework

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.

Solution 8 - Entity Framework

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.

Solution 9 - Entity Framework

> 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>

Solution 10 - Entity Framework

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)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionarame3333View Question on Stackoverflow
Solution 1 - Entity FrameworkSten PetrovView Answer on Stackoverflow
Solution 2 - Entity FrameworkLadislav MrnkaView Answer on Stackoverflow
Solution 3 - Entity FrameworkPascal CarmoniView Answer on Stackoverflow
Solution 4 - Entity FrameworkIzzyView Answer on Stackoverflow
Solution 5 - Entity Frameworkarmstb01View Answer on Stackoverflow
Solution 6 - Entity FrameworkAlex RodriguezView Answer on Stackoverflow
Solution 7 - Entity FrameworkGraham WalkerView Answer on Stackoverflow
Solution 8 - Entity Frameworkswati dhamanaView Answer on Stackoverflow
Solution 9 - Entity Frameworknazim hatipogluView Answer on Stackoverflow
Solution 10 - Entity FrameworkClarenceView Answer on Stackoverflow