huysentruitw / entity-framework-core-mock

Easy Mock wrapper for mocking EFCore5 DbContext and DbSet using Moq or NSubstitute

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

mikesalway opened this issue · comments

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?

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