A Asp.Net WebApi application consist of following classes Movie and Song. (one to many relationship)
public class Movie : PropertyChangedBase //(Implementation of INotifyPropertyChanged)
{
public Movie()
{
Songs = new ObservableCollection<Song>();
}
private int? _id;
public int? Id
{
get { return _id; }
set
{
_id = value;
NotifyOfPropertyChange(() => Id);
}
}
private string _title;
public string Title
{
get { return _title; }
set
{
_title = value;
NotifyOfPropertyChange(() => Title);
}
}
private ICollection<Song> _songs;
public ICollection<Song> Songs
{
get { return _songs; }
set
{
_songs = value;
NotifyOfPropertyChange(() => Songs);
}
}
}
public class Song : PropertyChangedBase //(Implementation of INotifyPropertyChanged)
{
private int _id;
public int Id
{
get { return _id; }
set
{
_id = value;
NotifyOfPropertyChange(() => Id);
}
}
private string _title;
public string Title
{
get { return _title; }
set
{
_title = value;
NotifyOfPropertyChange(() => Title);
}
}
}
Web Api PUT Method:
// PUT: api/Movie/5
[ResponseType(typeof(Movie))]
public IHttpActionResult PutMovie(int id, Movie movie)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
if (id != Movie.ID)
{
return BadRequest();
}
db.Entry(Movie).State = EntityState.Modified;
try
{
db.SaveChanges();
}
catch (DbUpdateConcurrencyException)
{
}
return Ok(movie);
}
When creating a new record, Movie and collection of songs created successfully. Its possible to edit(update) the values of class Movie.
Im calling PutMovie method to update the existing records.
Problem: 1) When adding new songs to the existing collection, no changes got updated. No errors but no song rows created in DB.
2) When updating existing values in Song, no changes got updated. No errors but no song values modified in DB.
Note: I'm using a c# client to consume web api.
Please help me to solve this.
DbUpdateConcurrencyException? If so, you have a problem that is causing your save to fail. Simply putting an empty catch to hide the exception isn't going to make the save magically succeed...