huysentruitw / entity-framework-core-mock

Easy Mock wrapper for mocking EFCore5 DbContext and DbSet using Moq or NSubstitute
MIT License
132 stars 24 forks source link

How do I call OnModelCreating #19

Closed integral-llc closed 3 years ago

integral-llc commented 5 years ago

All of me Entities do not use attributes to mark the keys, everything is happening inside OnModelCreating override method. i.e.

modelBuilder.Entity<MarketplaceEntity>(entity =>
            {
                entity.ToTable("Marketplace", "Application");
                entity.HasKey(e => e.MarketplaceKey);
            });

How to call this method so the keys are properly initialized? As of now, I'm getting an exception saying the entity does not contain any property marked with KeyAttribute.

thx!

huysentruitw commented 5 years ago

Unfortunately entity configurations like that are currently not supported. As of now, I can't tell how complex it would be to add support for it. However, currently, you can work-around this by passing your own key-factory to the call to CreateDbSetMock:

dbContextMock.CreateDbSetMock(x => x.MarketPlaces, (e, _) => e.MarketplaceKey, initialEntities);