Reputation: 24308
I am trying to create 3 tables represented by pocos for use in code first.
The tables are customer, item and orders. (i presume the orders is a good naming convetion? or should i use customitem, or customeritem?)
I have created a customer table with an Id and things like Name, Address
I have created an item table with an Id and things like itemname and price.
Now i need to create a 3rd table which will hold the number of items a customer has purchased. I have an Id so i need to create 2 foreign keys (int) 1 called CustomerId and the other called ItemId - is this correct?
Then i need to create a navigation property from customers so i can see all the items that the customers has purchased
and navigation property for an item so you can see which customers has purchased that item
I am a little lost of how to do this
Does anyone have an example or can help me?
I presume its a standard question, how to create customers and items and store who purchased what but i can't seem to find any tutorials
Thanks in advance
Upvotes: -1
Views: 421
Reputation: 9458
You don't really need the third table,Entity framework
will create the third table automatically and the relationship will be managed internally.Only you have to put the Principal and dependent
in the code using Navigation Properties.
public partial class Customer
{
public Customer()
{
this.Items = new HashSet<Item>();
}
public int CustomerId { get; set; }
public string CustomerName { get; set; }
public string CustomerAddress { get; set; }
public virtual ICollection<Item> Items { get; set; }
}
public partial class Item
{
public int ItemId { get; set; }
public int Price { get; set; }
public int CustomerCustomerId { get; set; }
public virtual Customer Customer { get; set; }
}
Thus , you can establish the relationship between the two tables, customer
and item
. But if you want to add some descriptive attributes to the relation , then you should go creating new table in entity framework which will include foreign keys from both Customer
and Item
classes and your additional discriptive attributes like PurchaseDate
etc
Upvotes: 2