Home > database >  Why Scaffold-DbContext doesn't put set method set inside relational tables entity class
Why Scaffold-DbContext doesn't put set method set inside relational tables entity class

Time:01-31

When I used Scaffold-DbContext in EF Core 6, the entity Classes created with set enabled :

public class Employee
{  
    public int Id { get; set; }
    public string? FirstName { get; set; } 
    public string? LastName { get; set; } 
                                                             
    public virtual ICollection<EmployeeDatils> EmployeeDatils{ get; set; }
}

But in EF Core 7, the entity Classes created without set :

public class Employee
{  
    public int Id { get; set; }
    public string? FirstName { get; set; } 
    public string? LastName { get; set; } 
                                                             
    public virtual ICollection<EmployeeDatils> EmployeeDatils { get;} = new List<EmployeeDatils>();
}

So, I can't put data in EmployeeDatils Class. Because it is read-only property and it created only with get; method.

And also, I use this code to scaffold :

Scaffold-DbContext "Data Source=myIP; UID=myUser; Password=myPassword;Database=myDB;" Microsoft.EntityFrameworkCore.SqlServer –OutputDir Db -NoPluralize -UseDatabasenames -f

How can I fix this?

CodePudding user response:

You can use the T4 templates to change this to a not recommended pattern.

see explanation from the EF Core team here

  • Related