MapsterMapper / Mapster

A fast, fun and stimulating object to object Mapper
MIT License
4.3k stars 328 forks source link

Mapster.EF6 plugin .BuildAdapter exception #511

Open ktodyruik opened 1 year ago

ktodyruik commented 1 year ago

Hi,

I'm new to trying Mapster with EF6 and for select queries it has been great. I'm trying it out with updates and this seems like it should be pretty straightforward. I was hoping to be able to update my root entity fields without re-mapping the child collection which caused unnecessary inserts. I installed Mapster.EF6 2.0.0, followed the examples on the Wiki page.

var userEntity = db.Users.Where(x => x.UserID == userToSave.UserID).FirstOrDefault();
userToSave
    .BuildAdapter(config)
    .EntityFromContext(db);

For clarity I removed .AdaptTo(userEntity) because it wasn't getting there.

Throws the exception:

System.InvalidOperationException
Sequence contains no matching element
   at System.Linq.Enumerable.First[TSource](IEnumerable`1 source, Func`2 predicate)
   at Mapster.TypeAdapterBuilderExtensions.<>c__DisplayClass0_0`1.<EntityFromContext>b__0(TypeAdapterConfig config)
   at Mapster.TypeAdapterConfig.<>c__DisplayClass99_0.<Fork>b__0(String _)
   at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
   at Mapster.TypeAdapterConfig.Fork(Action`1 action, String key1, Int32 key2)
   at Mapster.TypeAdapterBuilder`1.ForkConfig(Action`1 action, String key1, Int32 key2)
   at <snip>Repositories.UserRepository.SaveUpdateUser(Int32 updatingUserId, User userToSave) in <snip> Repositories\UserRepository.cs:line 198
<snip> at UnitTests.Repositories.UserRepositoryTest.UpdateUserDetails() in <snip>\Repositories\UserRepositoryTest.cs:line 364

Would appreciate any suggestions.

Thanks, Kerry