I have a fairly simple many to many relationship set up in my DB from which I managed to generate
Where I need to be able to create relationships between users and orders (assigning users to orders).
It all seems fine, but when I try calling
db.Orders.Add(order);
// Set all attached users to unchanged
db.ChangeTracker.Entries<AspNetUser>().ToList()
.ForEach(t => t.State = System.Data.Entity.EntityState.Unchanged);
try
{
db.SaveChanges();
}
I get this annoying error
"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."
With innerException
"Invalid object name 'dbo.OrderAspNetUsers'."
I created OrderAspNetUsers
with
CREATE TABLE [dbo].[OrdersAspNetUsers] (
[AspNetUserId] nvarchar(128) NOT NULL FOREIGN KEY references [dbo].[AspNetUsers],
[OrderId] INT NOT NULL FOREIGN KEY references [dbo].[Orders],
primary key (AspNetUserId, OrderId)
)
I did notice that weirdly, the error above references invalid object name OrderAspNetUsers
, but the table in my DB is definitely called OrdersAspNetUsers
.
Is this some weird issue with pluralisation?
Any help or advice is much apprecated, I really have no idea what to do here