Asp.Net WebApi 2 PUT 方法未更新嵌套 collection

Asp.Net WebApi 2 PUT method not updating nested collection

一个 Asp.Net WebApi 应用程序包含以下 classes 电影和歌曲。 (一对多关系)

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 方法:

// 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);
        }

创建新记录时,电影和 collection 首歌曲创建成功。 可以编辑(更新)class Movie.

的值

我正在调用 PutMovie 方法来更新现有记录。

问题: 1) 将新歌曲添加到现有 collection 时,没有更新任何更改。没有错误,但没有在数据库中创建歌曲行。

2) 更新 Song 中的现有值时,未更新任何更改。没有错误,但没有在数据库中修改歌曲值。

注意:我正在使用 c# 客户端来使用网络 api。

请帮我解决这个问题。

here您可以阅读:

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.

也就是说你的歌曲被认为是Unchanged。这就是它们没有更新的原因。

我在使用 .Net Core 2.x 和 Entity Framework Core 2.2.1 时遇到了这个问题。 我发现除了将嵌套实体标记为已修改外,我还必须将嵌套实体附加到 DbContext,因为它不会从 .Include() 调用中自动跟踪。

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