且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

忽略更新实体框架上的一个属性

更新时间:2023-02-13 11:01:21

我个人使用AutoMapper并在不想复制的一个字段上使用忽略。这样,如果您将新列添加到表中,就不必担心将它们添加到存储库中。

Personally, I use AutoMapper and use Ignore on the one field you don't want to copy. This way, if you ever add new columns to the table, you don't have to worry about adding them to your repository.

所以,像这样...

void UpdatePerson(Person p) {
    var person = db.Persons.Find(PersonId);

    Mapper.CreateMap<Person, Person>()
        .ForMember(dest => dest.CreationDate, opt => opt.Ignore());
    Mapper.Map(p, person);        

    db.SaveChanges();        
}