EF collections



  • There are three EFs:

    public User
    {
      public int ID {get;set;}
      public string Name {get;set;}
      public virtual ICollection<Permission> Permissions;
    }
    

    public Permission
    {
    public int ID {get;set;}
    public string Name {get;set;}
    public virtual ICollection<User> Users;
    public virtual ICollection<Domain> Domains;
    }

    public Domain
    {
    public int ID {get;set;};
    public string Name {get;set;}
    public int PermissionID {get;set;}
    public Permission Permission {get;set;}
    }

    The draft uses Code First. As you can see, the essence of the relationship is:

    User**(M)-(M)Permission

    Permission(1)-(M)**Domain

    There are generic repositories. I get the user collection from the context as follows:

    List<User> AllUsers = context.GetAllIncluding<User>(u=>u.Permissions).ToList();

    I receive a user collection, each element of which includes a collection of copies of Class Permission, and the reference to a collection of Domain copies already contains null.

    Question: As a user collection, each element of which will contain the Permissions collection, each element of which in turn will contain the Domains collection, etc.



  • to use lazy loading Virtual properties must be made.


    In your particular case:

    public User
    {
      public int ID {get;set;}
      public string Name {get;set;}
    

    public virtual ICollection<Permission> Permissions {get;set;}
    }

    public Permission
    {
    public int ID {get;set;}
    public string Name {get;set;}

    public virtual ICollection<User> Users {get;set;}
    public virtual ICollection<Domain> Domains {get;set;}
    }

    public Domain
    {
    public int ID {get;set;};
    public string Name {get;set;}
    public int PermissionID {get;set;}

    public virtual Permission Permission {get;set;}

    }

    Squasts marked with the retrofit virtualso-called navigational properties to which Entity Framework pull the necessary records automatically




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2