本文介绍了亚音速和Automapper - dirtyColumns集合为空,因此无法更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的亚音速3 Automapper上一个asp.net MVC3项目。

I'm using Subsonic 3 and Automapper on an asp.net MVC3 project.

在我HttpPost的ActionResult,我以我的模型,并将其映射到我的亚音速生成的实体。

In my HttpPost ActionResult, I'm taking my model and mapping it to my Subsonic generated entity.

映射工作没有probs,但我不能更新实体。

The mapping works no probs, but I can't update the entity.

经过进一步检查,那是因为我有没有脏列,因此我调用Update()作为亚音速不认为它需要进行任何更新失败。

Upon further inspection, it is because I have no dirty columns, therefore my call to Update() fails as Subsonic doesn't think it needs to update anything.

我已经重新吉格舞的code加载 - 甚至迫使方法对模型映射前再次加载从DB的实体。这似乎只是映射破坏dirtyColumns跟踪。例如。如果我从数据库加载地图后,再改一个随机属性,它不会被标记为脏列。

I've re-jigged the code loads - even forcing the method to load the entity from the db again before mapping against the model. It just seems that the mapping destroys the dirtyColumns tracking. E.g. if I map after loading from the DB, and then change a random property, it doesn't get marked as a dirty column.

我还使用SetIsLoaded(true)方法调用尝试。映射后没有喜悦。

I've also tried using the SetIsLoaded(true) method call. No joy after mapping.

下面是我的方法:

    [HttpPost]
    public virtual ActionResult Edit(SinglePersonModel model)
    {
        if (ModelState.IsValid)
        {
            Data.Person person;

            //Now Map my model to my entity - this works
            Mapper.CreateMap<SinglePersonModel, Data.Person>();
            person = Mapper.Map<SinglePersonModel, Data.Person>(model);

            //THIS DOESN'T SET MY COLUMN TO DIRTY
            person.Link = "asdjsadij";

            //THIS DOESN'T SET MY COLUMN TO DIRTY EITHER
            person.SetIsLoaded(true);
            person.Link = "asdjsadij";

            if (person.PersonId > 0)
                PersonRepository.UpdatePerson(person);
            else
                PersonRepository.CreatePerson(person);

            return RedirectToAction(MVC.SecureAdministration.Person.Index());
        }
        else return View(model);
    }

在我PersonRepository的静态方法,只是分别致电亚音速的更新()和Save()。

The Static methods on my PersonRepository just call subsonic's Update() and Save() respectively.

任何的想法会更AP preciated。现在我想,我可能需要把一些附加属性到我的模型,以确保他们得到的automapper结转到实体。

Any ideas would be much appreciated. I'm now thinking that I may need to put some additional properties into my model to make sure that they get carried over into the entity by the automapper.

在最坏的情况下我必须映射回从模型,这将吸实体时,只是没有使用Automapper。

In the worst case I'll have to just not use the Automapper when mapping back to entities from the model, which would suck.

推荐答案

AutoMapper.Mapper.Map&LT; SinglePersonModel,Data.Person&GT;(模型,人); - 有你试过这样吗?这并不分配对象的新实例,但其分配给现有的对象。只是一个想法。我理解不是从数据库加载它的缺乏。但想通这可能有点帮助:)

AutoMapper.Mapper.Map<SinglePersonModel, Data.Person>(model, person); - Have you tried it like this? This doesn't assign a new instance of the object but assigns it to the existing object. Just a thought. I understand the want of not loading it from the db. But figured this might help a bit :)

感谢您的 - 很高兴帮助了:)

Thanks for that - glad to help out :)

这篇关于亚音速和Automapper - dirtyColumns集合为空,因此无法更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 16:02