1

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.

2
  • Are you getting a 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... Commented Mar 24, 2015 at 20:57
  • I'm sorry. I have handled the exception but i didn't include the code in above example. Commented Mar 25, 2015 at 5:30

2 Answers 2

2

from here you can read:

Note that if the entity being attached has references to other entities that are not yet tracked, then these new entities will attached to the context in the Unchanged state—they will not automatically be made Modified. If you have multiple entities that need to be marked Modified you should set the state for each of these entities individually.

that is your songs are considered as Unchanged. That is why they are not updated.

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

6 Comments

Thanks. You mean something like this: db.Entry(Song).State = EntityState.Modified; ?
indeed. You have to parse the tree.
Thanks now its work fine. Btw is there any configuration to automatically track changes in EF?
By track, you mean audit the database ?
I mean, is there any alternative way to set EntityState without manually setting the EntityState for each entities. Do you know the use of context.Configuration.AutoDetectChangesEnabled = true ?
|
1

I had this problem when using .Net Core 2.x with Entity Framework Core 2.2.1. I found that in addition to marking the nested entity as Modified, I also had to attach the nested entity to the DbContext, as it wasn't being tracked automatically from an .Include() call.

db.Attach(movie.Song);
db.Entry(movie).State = EntityState.Modified;
db.SaveChanges();

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.