0

I am receiving this error: "A relationship multiplicity constraint violation occurred: An EntityReference can have no more than one related object, but the query returned more than one related object. This is a non-recoverable error."

It happens during a loop through a set of objects, in this case when it gets to the second one in the loop. The error seems to indicate that one of the reference properties of the object in the list I am looping through is throwing this error. I know that the data is correct on the database side, so I'm not sure what Entity Framework is seeing here that it doesn't like.

This is the fluent configuration in question:

modelBuilder.Entity<TemplateSubscriber>()
            .HasRequired(r => r.Role)
            .WithOptional()
            .Map(m => m.MapKey("RoleID"))
            .WillCascadeOnDelete(false);

This particular configuration allowed me to get the correct SQL generated in my DbMigration, and I am happy with the database, however something must be wrong with how EF sees this relationship, because it throws this error when it tries to load up a second "TemplateSubscriber" in a loop, and that error is seen specifically on trying to load the "Role" reference property.

The relationship is a one to many, with the relationship accessible only from the one side. I have no need to access a list of the templateSubscribers from within the role. So what I wanted was a foreign key relationship so that the templateSubscriber must reference an actual role.

What could be the issue here? And how can I see what the SQL statement is that is erroring out, I would probably be able to diagnose if I could see the SQL.

4

2 回答 2

1

This was the answer:

https://stackoverflow.com/a/9269397/1296464

Needed to change WithOptional() to WithMany()

It didn't make a change in the DB, but something under the hood is better now.

于 2013-05-28T14:02:14.563 回答
0

I had a similar issue. Mine was a result of using LazyLoading and not having all my navigation properties set to Overridable (c# virtual). This error occurred when having one end of the navigation set as Overridable and the other not.

于 2017-04-14T20:18:08.673 回答