InsertOrUpdate > 插入确定,更新不工作

InsertOrUpdate > Insert OK, Update not working

几天前,我尝试使用 Entity Framework Core 的 AutMapper,现在我想看看 AutoMapper.Collection.EntityFrameworkCore (v1.0.1) 中的 InsertOrUpdate 是如何工作的。 但我无法启动它,这是我的测试-类/设置:

DataContext (DbContext)

public class DataContext : DbContext
{
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseLazyLoadingProxies(false).EnableSensitiveDataLogging(AppHelper.ShowDebugInfos).UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking).UseSqlServer(AppHelper.ConnectionString);
    }

    public DbSet<Contact> Contacts { get; set; } // with some references to other tables
}

启动

services.AddDbContext<DataContext>();

services.AddEntityFrameworkInMemoryDatabase().AddDbContext<DataContext>(); // not sure if we need this for InsertOrUpdate

services.AddAutoMapper(automapper =>
{
    automapper.AddCollectionMappers();
    automapper.UseEntityFrameworkCoreModel<DataContext>(services);
    //automapper.SetGeneratePropertyMaps<GenerateEntityFrameworkCorePrimaryKeyPropertyMaps<DataContext>>(); // exception says we have to use UseEntityFrameworkCoreModel instead of this
}, typeof(DataContext).Assembly);

AutoMapperProfile

public class AutoMapperProfile : Profile
{
    CreateMap<ContactTestDto, Contact>().ReverseMap();

    // did also try:
    //CreateMap<ContactTestDto, Contact>().EqualityComparison((source, destination) => source.Id == destination.Id);
    //CreateMap<Contact, ContactTestDto>().EqualityComparison((source, destination) => source.Id == destination.Id);
}

模特联系方式

public partial class Contact
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    // many more (including navigation properties)...
}

型号 ContactTestDto

public partial class ContactTestDto
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    // nothing more
}

更新

// this makes no difference:
//Contact contactFromDb = db.Contacts.Find(1); // FirstName is "John"
//db.Entry(contactFromDb).State = EntityState.Modified;

// nothing happens:
db.Contacts.Persist(mapper).InsertOrUpdate(new ContactTestDto { Id = 1, FirstName = "Updated Name???" });

// insert ok:
db.Contacts.Persist(mapper).InsertOrUpdate(new ContactTestDto { FirstName = "Inserted Name" });

db.SaveChanges();

我可以看到Select-声明两个案例(通过SQL Profiler),但是在更新时没有发送更新,插入很好。 mapper 实例也工作正常(我可以双向映射)。

所以,我做错了什么?

PS:为什么我不能写"Hello SO-Community"?它将被缩减为 "Community" oO

您已在您的数据库选项 (.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking)) 中禁用跟踪,因此您需要通过选项重新启用它,或者为这样的请求启用它:

var currTracking = db.ChangeTracker.QueryTrackingBehavior;
try
{
    db.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.TrackAll;
    db.Contacts
        .Persist(mapper)
        .InsertOrUpdate(new ContactDto { Id = 1, FirstName = "Updated Name???" });
    db.Contacts.Persist(mapper).InsertOrUpdate(new ContactDto { FirstName = "Inserted Name" });
    db.SaveChanges();
}
finally
{
    db.ChangeTracker.QueryTrackingBehavior = currTracking;
}

P.S。 不确定是否需要通过 try-finally 设置回 currTracking,但安全总比抱歉好。