Home > OS >  EF Core 6 to 7 the collections no longer have setters
EF Core 6 to 7 the collections no longer have setters

Time:11-28

After upgrading from .net core 6 to 7 and rolling forward all the libraries, after scaffolding the database(we use db first) a lot of the generated properties no longer have setters. These were present before and heavily used. I was looking for options on the scaffold command to include setters or any alternative method to make sure this happens

EF Core 6

public virtual ICollection<AccountsPurchaseInvoiceLines> AccountsPurchaseInvoiceLines { get; set; } = new List<AccountsPurchaseInvoiceLines>();

EF Core 7

public virtual ICollection<AccountsPurchaseInvoiceLines> AccountsPurchaseInvoiceLines { get; } = new List<AccountsPurchaseInvoiceLines>();

The lack of the setter on all the entities is the problem. I can go and add manually to get the project building again but I can't find a way to make sure next time I generate the entities the setters remain.

Thanks

CodePudding user response:

Take a look at the documentation.
Customizing the model.

Starting in EF7, you can also use T4 text templates to customize the generated code.

Customize the entity types.

Add a setter to the T4 template.

CodePudding user response:

You do not need setters here.

In a DB first project, EF will set the collections for you.

You don't use setters to add to collections, you use getters to get the collection then add to the retrieved collection using the add method.

MyCollection.Add(myEntry); and MyEntity.MyCollection.Add(myEntry); do not use any setters - they use a getter and an add method.

If you use setters to add to collections then you are replacing the whole collection each time instead of just adding to them.

If your code needs the setter for some reason, it is doing something very wrong.


If you want to get the setters back just to avoid rewriting a load of code then you could simply have to downgrade back to EF 6.

Update:

After seeing Alexander Petrov's answer, it looks like you can also customise the model using T4 text templates.

CodePudding user response:

We have managed to reinstate the setters for now until the code base can be addressed in accordance with the above helpful comments

  • Related