Entity Framework adding record with a related object issue

beseseigree

New Member
We have the following AddUser method which adds a new record to the database using EF:\[code\] public User AddUser(User user) { using (var context = DataObjectFactory.CreateContext()) { var userEntity = Mapper.Map(user); context.AddObject("UserEntities", userEntity); context.SaveChanges(); return Mapper.Map(userEntity); } }\[/code\]Our User type is like this:\[code\]public class User{ public string FirstName { get; set; } public string LastName { get; set; } public Customer Customer { get; set; } }\[/code\]We have a Customer table and a User table and in User table we have customerid as a foreign key. In the USER table we have Firstname, Lastname and CustomerId fields, we get the firstname, lastname and customerid (dropdown list of customers) details from the user, create the Customer object with customerid we get from the user and pass all of these to the \[code\]AddUser\[/code\] method, but what happens is that the User gets inserted into the database and also a new Customer is inserted into the Customer's table (with a new customerid), is there any way of stopping this?Thanks
 
Back
Top