2

I have a fairly simple many to many relationship set up in my DB from which I managed to generate

enter image description here

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

Bassie
  • 9,529
  • 8
  • 68
  • 159
  • Please try to call for a db.SaveChanges() after you run db.Orders.Add(order). You can then call for the ChangeTracker line of code and save again. I believe you are running into a concurrency issue where you add an order which includes a AspNetUser as a property but then state it is unchanged before you save it. I would write it as an answer but this is more a hunch than an answer. – sjgallen Nov 23 '17 at 05:19
  • @sjgallen Thanks for your comment. I originally tried it like that but got `"Violation of PRIMARY KEY constraint 'PK_dbo.AspNetUsers'. Cannot insert duplicate key in object 'dbo.AspNetUsers'. The duplicate key value is (3197712e-0c39-42ee-a596-6dc390898eb8).\r\nThe statement has been terminated."` when calling `SaveChanges()` - looks like it is trying to add the users as new records, but they already exst in the db – Bassie Nov 23 '17 at 05:26
  • Thank you for trying and the added information. I am going to do some research on the errors you are seeing and may be some N+1 problems just in case that is an answer. I up voted the question to help you find an answer. I am interested in the solution. – sjgallen Nov 23 '17 at 05:36
  • @sjgallen Thanks so much, let me know if I can do anything to help – Bassie Nov 23 '17 at 05:41
  • Have you seen this question/answer? https://stackoverflow.com/questions/7938384/an-error-occurred-while-saving-entities-that-do-not-expose-foreign-key-propertie It has several possible answers. The second one is interesting but review them all. – sjgallen Nov 23 '17 at 05:51
  • @sjgallen Note after I manually added an order with no users, I can't view it because `Invalid column name 'AspNetUser_Id'.`, although that is not the name of my FK column (It should be looking for `AspNetUserId`) – Bassie Nov 23 '17 at 05:59

0 Answers0