0

I am working on a code first .NET application that has an auto generated table. I created my Order class, with a link to Item. Instead of manually creating an OrderItem class, I simply used List<Item> and it auto generated the OrderItem table for me;

[Table("Orders")]
public class Order
{
    [Key]
    public int OrderId { get; set; }

    public virtual List<Item> Items { get; set; }

    public string OrderNumber { get; set; }      

}

This, as expected has made a table in the database called OrderItems

Many years later, we now want to reference OrderItems from another table within entity framework. However, as it is not a class we can physically reference, we are not sure how to do it in code. Obviously changing the database is easy, but that won't help for code first.

I'd like to do something like the following

[Table("AnotherTable")]
public class AnotherTable
{
    [Key]
    public int AnotherTableId { get; set; }

    public string SomethingHere { get; set; }     

    public virtual OrderItem OrderItem { get; set; } 

}

Which I cannot do, as OrderItem isn't a class it understands.

Is my only alternative to try and recreate in code, what EF auto created for me? I.e

[Table("OrderItems")]
public class OrderItem
{   
    public virtual Order Order_OrderId { get; set; } 
    public virtual Item Item_ItemId { get; set; } 

}
KittenKiller
  • 117
  • 9

0 Answers0