Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disabling Entity Framework's default value generation (Code First)

I have a column in the database that cannot be null, and I want to set it to have a default value in the database . The problem is that entity framework seems to create a default value itself (for example, int => 0), and completely ignores the default value constraint in the database.

Is there any way to disable this default valuing of entity framework?

like image 307
Kornél Regius Avatar asked Jul 27 '11 15:07

Kornél Regius


People also ask

How can you disable lazy loading from Dbcontext constructor?

We can disable lazy loading for a particular entity or a context. To turn off lazy loading for a particular property, do not make it virtual. To turn off lazy loading for all entities in the context, set its configuration property to false.

What is Entity Framework core code first?

The Code First approach enables you to define an entity model in code, create a database from the model, and then add data to the database. MySQL Connector/NET is compatible with multiple versions of Entity Framework Core.

What is ValueGeneratedOnAdd?

The Entity Framework Core Fluent API ValueGeneratedOnAdd method indicates that the value for the selected property is generated by the database whenever a new entity is added to the database. Therefore, the property should be ignored by EF Core when constructing an INSERT statement.


1 Answers

I have found that you can decorate your fields with the following attribute.

[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
like image 197
KnightHawk Avatar answered Sep 24 '22 14:09

KnightHawk