'.NetCore XUnit access ModelBuilder object of EF DataContext outside DataContext class

I have .Net Core WebApi Application, for which I am writing XUnit tests for Controllers with InMemoryDatabase approach.The actual controllers are injected with DataContext object.

We have an Entity of similar type in our Database Entities.

public class EntityA
{
    [Key]
    public int Id { get; set; }
    public object Parameters { get; set; }
}

And since the Parameters property is of type object , the XUnit tests throw below error for the DataContext during test setup.

System.InvalidOperationException : The property 'EntityA.Parameters' could not be mapped because it is of type 'object', which is not a supported primitive type or a valid entity type. Either explicitly map this property, or ignore it using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.

So I somehow need to access the ModelBuilder object that is available in OnModelCreating(ModelBuilder builder) method of the DataContext, so I can add the code to ignore this mapping from the test related code changes.

Code to ignore this property during Test Execution is as follows: builder.Entity<EntityA>().Ignore(x => x.Parameters);

We do not intend to make any changes to the actual DataContext , just to ignore this property from the Test Perspective.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source