Search code examples
entity-frameworksuperclasscircular-reference

Entity Framework Base Class


Having a base class for entity objects I am being faced with the problem of circular reference and base Dependent Role introduction problems wherever I have a composite class of entities:

public abstract class Base
{
   public int Id {get;set;}
   public DateTime CreationTime {get;set;}
   public User Creator {get; ste;}
}
public class User : Base
{
   public string Name {get;set;}
   public Country BirthPlace {get;set;}
   // rest of the properties
}
public class Country : Base
{
   public string Description {get;set;}
   //rest of the properties
}

Any idea of how to overcome that?


Solution

  • create a Mapping class to ignore base class user

    public class UserMap : EntityTypeConfiguration<User>
    {
        public UserMap()
        {
            Ignore(x => x.User);
        }
    }
    

    or

    remove User from base and add it in every domain that will need it (example)

    public class Country : Base
    {
       public string Description {get;set;}
       public User Creator {get; set}
       //rest of the properties
    }