IM using c# and MVC and the entity framework, now i have an entity that i am passing through to the model of the view for an update, my edit post back takes back the entity and a few extra fields that i am using editors for. i have a repository manager and it has a entity context, however when i call savechanges on my repository base (shown below) it seems to be adding a new entity as opposed to updating the one that is coming through. and i have no idea why??
public void SaveChanges()
{
TransactionOptions options = new TransactionOptions()
{
IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted
};
using (TransactionScope scope = new TransactionScope(TransactionScopeOption.RequiresNew, options))
{
try
{
RepositoryManager.GetContext(GetContextType()).SaveChanges();
scope.Complete();
}
catch
{
scope.Dispose();
throw;
}
}
}
CORRECTION IT SEEMS TO BE UPDATING AND CREATING A NEW ONE
im doing the following in my controller
public ActionResult Edit(FlightInstance flight, string timeselector, string dateselector, int destinationlocation, int departurelocation) { try { flight.TimeOfFlight = DateTime.Parse(dateselector + " " + timeselector); flight.DestinationLocation = m_LocationRepository.GetLocation(destinationlocation); flight.DepartureLocation = m_LocationRepository.GetLocation(departurelocation); if (flight.IsValid) { m_FlightRepository.UpdateFlightInstance(flight); return View("List"); }
and ive resorted to the following to try and get it to update the object in
public void UpdateFlightInstance(FlightInstance flight)
{
var flights = (from flightInDb in Context.FlightInstance
.Include("DestinationLocation")
.Include("DepartureLocation")
where flightInDb.ID == flight.ID
select flightInDb).FirstOrDefault();
if (flights != null)
{
flights.Name = flight.Name;
flights.ID = flight.ID;
flights.EntityKey = flight.EntityKey;
flights.TimeOfFlight = flight.TimeOfFlight;
flights.Description = flight.Description;
flights.TimeOfFlight = flight.TimeOfFlight;
flights.DestinationLocation = flight.DestinationLocation;
flights.DepartureLocation = flight.DepartureLocation;
}
SaveChanges();
}