0

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

4

1 回答 1

2

您真的不需要第三张表,将自动创建第三张表,并且内部管理关系。您只需Entity frameworkPrincipal and dependentNavigation 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; }
}

这样,就可以建立两个表之间的关系,customer并且item。但是,如果您想为关系添加一些描述性属性,那么您应该在实体框架中创建新表,其中将包括来自两个CustomerItem类的外键以及您的其他描述性属性PurchaseDate

于 2013-01-06T10:34:16.233 回答