I have a simple method where I save the given customer information into the database using entity Framework:
public Customer AddCustomer(Customer customer)
{
using (var context = DataObjectFactory.CreateContext())
{
context.AddObject("CustomerEntities", Mapper.Map(customer));
context.SaveChanges();
return customer;
}
}
Customer type is very simple, it consists of Id, and Name of the customer, so when we want to save a customer I simply pass a customer object to AddCustomer method, at this point the Id is null and the Name field contains the name I want save to the database.
This works fine, the name gets inserted into the database, however what I want to do is to get the saved customer Id back and return to the calling function, is there anyway of achieving this?
EDIT:
This is the Mapper method used:
internal static class Mapper
{
internal static IList<Customer> Map(IEnumerable<CustomerEntity> entity)
{
return entity.Select(Map).ToList();
}
internal static Customer Map(CustomerEntity entity)
{
return new Customer
{
CustomerId = entity.CustomerId,
Name = entity.Name
};
}
internal static CustomerEntity Map(Customer customer)
{
return new CustomerEntity
{
CustomerId = customer.CustomerId,
Name = customer.Name
};
}
}
Thanks
customer.Idafter the call toSaveChanges?