简体   繁体   English

Asp.Net WebApi 2 PUT方法不更新嵌套集合

[英]Asp.Net WebApi 2 PUT method not updating nested collection

A Asp.Net WebApi application consist of following classes Movie and Song. 一个Asp.Net WebApi应用程序由以下类Movie和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: 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);
        }

When creating a new record, Movie and collection of songs created successfully. 创建新记录时,电影和歌曲集成功创建。 Its possible to edit(update) the values of class Movie. 可以编辑(更新)Movie类的值。

Im calling PutMovie method to update the existing records. 我正在调用PutMovie方法来更新现有记录。

Problem: 1) When adding new songs to the existing collection, no changes got updated. 问题:1)将新歌曲添加到现有收藏中时,没有更新。 No errors but no song rows created in DB. 没有错误,但没有在数据库中创建歌曲行。

2) When updating existing values in Song, no changes got updated. 2)更新Song中的现有值时,没有任何更改被更新。 No errors but no song values modified in DB. 没有错误,但没有在数据库中修改歌曲值。

Note: I'm using ac# client to consume web api. 注意:我正在使用ac#客户端使用Web api。

Please help me to solve this. 请帮我解决这个问题。

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 . 那就是你的歌被认为是Unchanged That is why they are not updated. 这就是为什么它们不更新的原因。

I had this problem when using .Net Core 2.x with Entity Framework Core 2.2.1. 将.Net Core 2.x与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. 我发现,除了将嵌套实体标记为Modified之外,我还必须将嵌套实体附加到DbContext,因为不会从.Include()调用中自动跟踪它。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM