Click here to Skip to main content
15,884,176 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I have Two master Tables

1. Customer Master contains
CustomerId (PK)
CustomerName

2. Location Master Contains
LocationId (PK)
LocationName

And another table like

CustomerLocation contains
CustomerId (FK)
LocationId (FK)

When i insert ADO.NET Data model it will create entity for Customer and Location table only
if i want to insert data in CustomerLocation table. How can i insert?
I have one form which contain two dropdown one for Customer and one for Location
with which i have to insert data in CustomerLocation Table

I follow Repository Structure
So, With the help of DataRepository file which contain
T Add(T entity);
void Update(T entity); methods
if i want to create data repository for CustomerLocation how can i create. Because there is no
entity model for that.

Help me for that. I am new for Repository Structure.
Posted
Comments
Sander Rossel 29-Jun-14 5:45am    
Good question. This is a problem I also encountered when first starting with the Entity Framework. Please see my solution for a full explanation :-)

hi please refer this link for add[^] , Update[^] and Delete[^] code for ur understanding

if you want to perform this using store procedure use this deference link[^]
 
Share this answer
 
Comments
Sander Rossel 29-Jun-14 5:44am    
Your answer does not help the op. The problem is not that he doesn't know how to add or update, the problem is that the table is not created in the model.
Please see my answer for an explanation of the problem and a solution.
When Entity Framework locates a link table that only holds foreign keys to two other tables it will omit the table from the model and instead create an n-to-m relation on the tables that are linked.
So in your model you would expect:
- Customer has n CustomerLocations
- Location has n CustomerLocations
- CustomerLocation has 1 Customer
- CustomerLocation has 1 Location

Instead it creates the following:
- Customer has n Locations
- Location has n Customers

In this case to create a CustomerLocation it would be enough to add a Location to the Customers Locations or add a Customer to the Locations Customers.

If you really need the CustomerLocation table consider giving it an extra column, for example an ID field.
In this case your Entity Model won't create the n-to-m relation on Customer and Location, but instead create the CustomerLocation table and give the 'expected' output.

I know this because I've had the same 'problem' when I first started using Entity Framework :)

Here's a link that also explains this: Lack of a many-to-many linking table in EF 4.0 a bug or a feature?[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900