0

Hi everyone I just started work as .netcore developer. They are using .net core with ef core. I saw some entitys has got constructor. I wonder whats that means. It's be like

using Project.Entities.CompanyEntity;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Text;
namespace Project.Entities.Entity1
{
    [Table("E_Entity1")]
    public class EntityCategory : EntityBase
    {
        public EntityCategory()
        {   //What is does makes. It fill theese data when i call "get"  with DbContext
            OtherCategoryBranches = new List<OtherCategoryBranch>();
            AnotherCategories = new List<AnotherCategory>();
            
        }
        public Guid Id { get; set; }
        public string Name { get; set; }
        public string NormalizedName { get; set; }
        public string Description { get; set; }
        public Guid CompanyId { get; set; }

        public virtual Company Company { get; set; }
        public virtual List<OtherCategoryBranch> OtherCategoryBranches { get; set; }
        public virtual List<AnotherCategory> AnotherCategories { get; set; }   
      
    }
}

//in other entity,"Other" ,Related to EntityCategory
namespace Project.Entities.OtherCategory
{
    [Table("E_OtherCategoryBranches")]
    public class OtherCategoryBranches : EntityBase
    {
        public Guid Id { get; set; }

        public Guid EntityCategoryId { get; set; }         

        public virtual EntityCategory EntityCategory { get; set; } 
    }
}

2

In SSIS DatabaseDiagram the Entity1 table is enter image description here

THe other related List's is for one to many right. And when call the get meton in DbContext it fill automaticly ?

pandakun
  • 55
  • 9
  • It just initiates `one-to-many` or `many-to-many` [Navigation Property](https://learn.microsoft.com/en-us/ef/core/modeling/relationships?tabs=fluent-api%2Cfluent-api-simple-key%2Csimple-key) to avoid null references – Artur Jan 07 '21 at 13:13
  • Well okay i get it – pandakun Jan 07 '21 at 13:53

1 Answers1

1

Entity framework Core, "unfortunately" requires entities to have an empty constructor.

Let's say you'd have a class of Person for simplicity.

public class Person 
{
    // no constructor defined, will under the hood create a empty constructor for you 

    public string Name {get; set;}
    public virtual ICollection<Pet> Pets {get; set;}
}

public class Person 
{

    // no empty constructor defined for this class, will cause EF Core to throw an exception

    public Person(string name, IEnumerable<Pet> pets)
    {
      Name = name;
      Pets = pets;
    }

    public string Name {get; set;}
    public virtual ICollection<Pet> Pets {get; set;}
}

public class Person 
{
    
    // The empty constructor doesn't have to be public though, 
    // so to have some encapsulation and defend against object invariants, 
    //   you could have a protected empty constructor.

    protected Person()
    {

    }

    public Person(string name, IEnumerable<Pet> pets)
    {
      Name = name;
      Pets = pets;
    }

    public string Name {get; set;}
    public virtual ICollection<Pet> Pets {get; set;}
}

Hope this will give you some clarification.

as a final summary, the classes you've shared probably tries to avoid null reference exceptions on their list, and therefore adding constructors that ensures lists are newed up when a class gets instantiated.

this could also be achieved without actually declaring the constructor, as shown in these two examples.

public class Person 
{
    public Person()
    {
        Pets = new Collection<Pet>();
    }

    public string Name { get; set; }
    public virtual ICollection<Pet> Pets { get; set; }
}

// alternative:

public class Person 
{
    public string Name { get; set; }
    public virtual ICollection<Pet> Pets { get; set; } = new Collection<Pet>();
}

ˈvɔlə
  • 9,204
  • 10
  • 63
  • 89
Bjarke Handsdal
  • 219
  • 1
  • 6
  • Yes I got a little. Do we need construcotr with new'ing list when we got related entity. Some others does not has constructor( will under the hood create a empty constructor for you ) But in this releted entity does not have empty constructor – pandakun Jan 07 '21 at 13:21
  • you are thinking about the ``OtherCategoryBranches : EntityBase`` not having an empty constructor correct? it's probably because it doesn't have any lists as relations, so a constructor for newing up list relations is not necessary. – Bjarke Handsdal Jan 07 '21 at 13:31
  • Yes Entitybase is a class but not having any constructor except created automaticly. Also I have one more question I updated my post – pandakun Jan 07 '21 at 13:41