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

Is there anyway to get the effect of the [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)] annotation without using it

mlockett42 opened this issue · comments

My model is like this

public class MyModel
{
        [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }
        public string Name { get; set; }
}

But I want

public class MyModel
{
        public int Id { get; set; }
        public string Name { get; set; }
}

And to set the key and auto-increment functionality in CreateDbSetMock call.

I know I can do

var myMock = dbContextMock.CreateDbSetMock(x => x.MyModels, (e, k) => e.Id);

Which make Id the primary key (although it would be by default anyway) but that doesn't configure the auto-increment. What do I pass in for the CreateDbSetMock entityKeyFactory parameter to do this?

I managed to get this to work with.

var myMock = dbContextMock.CreateDbSetMock(x => x.MyModels, (e, k) => {
        e.Id = (int)k.NextIdentity;
        return e.Id;
});