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

It doesn't work with entities that have no key. #31

Closed mikesalway closed 3 years ago

mikesalway commented 3 years ago

I have an entity from a view, with no key available and is marked with (entity.HasNoKey();) I therefore can't create mock entities, I just get this error

No key factory could be created for entity type

Any suggestions?

huysentruitw commented 3 years ago

In case of keyless entities, you'll need to pass a key factory yourself.

This can be as simple as: (x, _) => x to use the entire model as key or (x, _) => x.Id if there is a property that can be used as a key (Id in this example).

The above is demonstrated in this unit-test