Click here to Skip to main content
15,885,435 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have data like
country state   city   pincode
 india   AP     Hyd     500001

now i can insert this as
id parentid   valtype  valtext
1    null       country  india
2    1          State     AP
3    2          City     Hyd
4    3         Pincode     500001


but i am unable to do this dynamically like if one more column is added to data like
country state   city   pincode    FamousPlace
 india   AP     Hyd     500001        Charminar


C#
<pre>
   id parentid   valtype  valtext
   1    null       country  india
   2    1          State     AP
   3    2          City     Hyd
   4    3         Pincode     500001
   5    4         FamousPlace   Charminar

</pre>



so how can i to do this please tell me the approach
Posted
Comments
Nathan Minier 7-Jan-16 7:34am    
It depends entirely on how your table is mapped. You can either add a child to an existing row, or add an existing row as a parent to a new one. Without knowing your mapping mechanism, there's not much that anyone will be able to tell you.

1 solution

First you should design the database properly (normalize) to have different types of tables with proper relationships

eg: Country Table, State Table
Relationship between country table and state table should be 1 : M (Many)

Then when you generate your EF data model, you should be able to add values easily for parent (country) and child (state) within the same transaction. It should be similar to below code sample.

Country c = new Country(){
    	name = "india"
    };
    
    State s = new State(){
    	state = "AP"
    };
    
    c.States.Add(s);
    
    DataContext.Countries.Add(c);
    DataContext.SaveChanges();
 
Share this answer
 
v2
Comments
Prasad Billupati 8-Jan-16 13:39pm    
but i would like to do with single table
Yasithl 25-Feb-16 1:38am    
This is not a limitation of Entity Framework, your table design is not correct.

Can you please share the sql statements which can be use to insert multiple records within single transaction for your table ?

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