I am currently using EF Core Code First Approach and I have the following domain classes:
public class Employee
{
// several properties such as Name, Address, ...
public Guid? CompanyId { get; set; }
public Company Company {get;set;}
}
public class Company
{
public Guid? CompanyId { get; set; }
public IEnumerable<Employee>? Employee{ get; set; }
}
I am using XUnit and Autofixture to create my tests:
public class MyTest{
private readonly Fixture _fixture = new Fixture();
[Fact]
public void TestMethod(){
var employee = _fixture.Create<Employee>();
...
}
}
The above _fixture.Create gives me the below error:
Message: AutoFixture.ObjectCreationExceptionWithPath : AutoFixture was unable to create an instance of type AutoFixture.Kernel.FiniteSequenceRequest because the traversed object graph contains a circular reference.
I need to include the: public Company Company {get;set;} in the Employee class to make it a foreign key. But if I remove that line, it works fine.
Many links online suggests the below solution - but I wanted to know if there is another way around instead of removing the ThrowingRecursion behavior:
fixture.Behaviors.OfType<ThrowingRecursionBehavior>().ToList()
.ForEach(b => fixture.Behaviors.Remove(b));
fixture.Behaviors.Add(new OmitOnRecursionBehavior());