Reputation: 73
I'm trying to make this many to may relation working , here is the schema
I have following configuration for my entities, first ContentBranch entity:
configuration.HasMany(e => e.ContentLeafs)
.WithMany(e => e.ContentBranches)
.Map(m => m.MapLeftKey("ContentLeafId").MapRightKey("ContentBranchId").ToTable("ContentBranchLeafs"));
Then ContentLeaf entity:
configuration.HasMany(e => e.ContentBranches)
.WithMany(e => e.ContentLeafs)
.Map(e => e.MapLeftKey("ContentBranchId").MapRightKey("ContentLeafId").ToTable("ContentBranchLeafs"));
I'm trying to execute following test code:
theBranch.ContentLeafs.Add(theLeaf);
await contentBranchRepository.Update(theBranch);
The problem is with the mapping I guess since I get following exception:
An error occurred while saving entities that do not expose foreign key properties for their relationships. The EntityEntries property will return null because a single entity cannot be identified as the source of the exception. Handling of exceptions while saving can be made easier by exposing foreign key properties in your entity types. See the InnerException for details.
Inner exception:
Invalid object name 'dbo.ContentLeafContentBranches'.
Does anyone know why EF is using convention when I'm providing explicit table name ? Please help.
Upvotes: 1
Views: 360
Reputation: 248
In Fluent Api you only have to set one side.
Try now with only this
configuration.HasMany(e => e.ContentLeafs)
.WithMany(e => e.ContentBranches)
.Map(m => m.MapLeftKey("ContentLeafId").MapRightKey("ContentBranchId").ToTable("ContentBranchLeafs"));
Upvotes: 2
Reputation: 73
It is my fault. My piece of code responsible for automatically registering type configurations has some issues and both answers above are correct.
Upvotes: 0
Reputation: 90
try to switch mapping keys in your entity configuration, that is: switch MapLeftKey with MapRightKey for ContentLeafs and you can do the same for Content branches. This solved your problem for me.
Upvotes: 2