0

I have a many-many relationship using EF database first between AspNetUser and Order:

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

So a user needs to select a bunch of usernames, and then I want to find these users in the DB and add them to the order (these users will take ownership of that order):

using (var db = new Entities())
{
    var users = db.AspNetUsers;
    var user = users.FirstOrDefault(u => u.UserName == userName);
    order.AspNetUsers.Add(user);
}

But I can see immediately after calling db.AspNetUsers, there is an innerException under users.ResultsView:

"Invalid column name 'Order_Id'."

There is no such column Order_Id in any of my tables or in my code, and I have no idea where to start debugging this.

Order.cs (generated from DB)

public partial class Order
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public Order()
    {
        this.OrderComments = new HashSet<OrderComment>();
        this.AspNetUsers = new HashSet<AspNetUser>();
    }

    public int Id { get; set; }
    public int OrganisationId { get; set; }
    public string Name { get; set; }
    public string OwnerId { get; set; }
    public string Description { get; set; }
    public decimal Value { get; set; }
    public string Status { get; set; }
    public string StatusDescription { get; set; }
    public Nullable<System.DateTime> DeadLine { get; set; }
    public Nullable<int> Progress { get; set; }

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<OrderComment> OrderComments { get; set; }
    public virtual Organisation Organisation { get; set; }
    public virtual AspNetUser AspNetUser { get; set; }
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<AspNetUser> AspNetUsers { get; set; }
}

Here I have an Owner AspNetUser, and then a list of other AspNetUsers who are also associated with that order.

Where is this Order_Id coming from? Please let me know if I need to add any more details

Bassie
  • 9,529
  • 8
  • 68
  • 159
  • Why have you not posted the code for order class? – CodingYoshi Nov 23 '17 at 02:11
  • @CodingYoshi i updated the question – Bassie Nov 23 '17 at 02:15
  • In your order class specify a primary key. That error you are getting is most likely because EF is making the assumption there is *Order_Id* in the class. – CodingYoshi Nov 23 '17 at 02:20
  • @CodingYoshi Thank you for your comments. In my database design, I already set `Id` as `Primary Key`. I guess from the generated code it looks like that hasn't been applied, do you know why that might be? – Bassie Nov 23 '17 at 02:22
  • @CodingYoshi Adding `[key]` manually to `Id` doesn't seem to have worked (same error) – Bassie Nov 23 '17 at 02:24
  • Sorry I just realized you are doing db first. Nonetheless, the issue is still the same but related to foreign keys. Please refer [this SO thread](https://stackoverflow.com/questions/19959256/entity-framework-invalid-column-name-id) which is about your issue. – CodingYoshi Nov 23 '17 at 02:30

0 Answers0