0

I'm using Silverlight5 with MVVM framework and Entity framework. In my project i have one doubt..

I have an entity named 'Customer' the structure is as follows..

Customer ocustomer = new Customer(); 
ocustomer.CustomerName = customername; 
ocustomer.CompanyName = company; 
ocustomer.AddressLine1 = address1; 
ocustomer.AddressLine2 = address2; 
ocustomer.City = city; 
ocustomer.State = state; 
ocustomer.Country = country; 
ocustomer.ZipCode = zipcode; 
ocustomer.Notes = note; 

_context.Customers.Add(ocustomer);

Now my need is to insert the Customer value into another table Named Customer_Entity

     Customer_Entity ocustomerEntity=new Customer_Entity ();
     ocustomerEntity.CustomerID=Customer_ID;
     ocustomerEntity.EntityTypeID =1;
     .
     .
     .
     .
     ocustomerEntity.CreatedTime =System.DateTime.Now;
     ocustomerEntity.CreatedBy=Common.ActiveData.Instance.userid; 

Here my need is to insert the customer value to Customer_Entity in every single row.. Customer_Entity Table Structure is as follows,

EntityID| CustomerID| EntityValue|
----------------------------------                                                                                                 
1       | 22        |jasper      |                                                                                  
2       | 22        |Company:Raj |
3       | 22        |Address     |

ocustomer.CustomerName=customername
.
.
.
ocustomer.CreatedTime=system.DateTime.Now..

so i need to insert all the values in every single row using unique CustomerID.. Need help to solve this one..

2
  • The problem is, I think, your model. How do you modeled that? Do you used FLUEN API? Commented Feb 21, 2013 at 12:53
  • No Fluen API.. I'm using Entity Framework Commented Feb 21, 2013 at 12:57

1 Answer 1

1

On your CustomerEntity object you should have a navigation property pointing to the corresponding Customer record. Similarly, I would expect that your Customer class has a collection of CustomerEntity on it as well.

In which case, you should instantiate the Customer object, populating with the necessary information. Don't add it to the DbSet yet, though. Afterwards, create all of the CustomerEntity records that you need to, connecting it to the Customer object by using the navigation property itself (i.e. NOT the ID field), and adding that CustomerEntity record to the corresponding DbSet in you Context class.

As a final step add your Customer object to the corresponding DbSet, and run SaveChanges. You should be good to go. Entity Framework should automatically generate the ID for you and populate it to the CustomerEntity records.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.