Hello, I have successfully mocked out a DBContext so that I can test my Entity Framework implementations, using the following lines:
//Initialize our source of ProductRequest MockedData
var productRequestMockSet = new ProductRequestMocks();
var productRequestData = productRequestMockSet.Initialize();
// Create a DbSet substitute.
var productRequestDbSet = Substitute.For<DbSet<ProductRequest>, IQueryable<ProductRequest>, IDbAsyncEnumerable<ProductRequest>>()
.SetupData(productRequestData);
//Initialize our Mocked DbContext
_context = Substitute.For<SOURCINGEntities>();
_context.ProductRequests.Returns(productRequestDbSet);
However when I add records to the ProductRequests entity, my PK field ID is not been auto incremented as it would if I was using the actual database (The ID field is set to auto increment within SQL). Is there anyway I can get this behavior to occur using NSubstitute, as some of my newer tests require this?
I have tried adding the [DatabaseGenerated(DatabaseGeneratedOption.Identity)] attribute to my Db Context, but this has no effect. And ideally I would like to avoid changing anything in the EF autogenerated files as they are overwritten every time the DB is refreshed.
Any help would be appreciated.
Kind regards
GianPiero