How to set another value on a boolean with defaut value with Entity Framework Core?

Korgoth

I got quite the same problem in this question : How to override SQL Server default value constraint on a boolean when inserting new entity? [closed]

Like him, I get the good value of my boolean from the client to the controller, false, but it's set to true by the call of _context.SaveChanges(); because of Entity Framework and the default value constraint in the database.

BUT : I'm using Entity Framework Core and I don't have any [DatabaseGenerated(DatabaseGeneratedOption.Computed)] annotation to remove to fix the problem.

In my ApplicationDbContext.cs :

modelBuilder.Entity<myEntity>(entity =>
{
    entity.Property(e => e.Active).HasDefaultValueSql("1");
    //entity.Property(e => e.Active).HasDefaultValueSql<bool>("1"); // doesn't fix
    ...
}

In my Database :

CREATE TABLE myEntity(
    Id      INTEGER IDENTITY(1,1) NOT NULL,
    ...
    Active  BIT NOT NULL CONSTRAINT DF_myEntity_Active DEFAULT 1
);

In my Controller :

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("Id, Active, etc.")] Entity myEntity)
{
    if (ModelState.IsValid)
    {
        _context.Add(myEntity); // here myEntity.Active = false
        await _context.SaveChangesAsync();
        // same problem with _context.SaveChanges(); in another controller
        // here myEntity.Active = true
    }
    ...
}

It seems that EF doesn't map C# boolean with SQL bit correctly and always takes the default value. Does somebody have an issue to force the false value ?

Korgoth

Finally I can have data annotations generated in my EF model using --data-annotations option in the EF command. So I put [DatabaseGenerated(DatabaseGeneratedOption.None)] on the property and don't use the default contraint of the database.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Error when set boolean value in entity

From Dev

How is this boolean value being set

From Dev

How to set null value to an existing attribute on Entity Framework

From Dev

argparse: Defaut value for nargs='*'?

From Dev

Spring @Scheduled defaut value

From Dev

How to set Entity Framework Core migration timeout?

From Dev

How to set default boolean value in JPA

From Dev

how to set @modelattribute boolean value inside html

From Dev

How to set false as a value using Boolean in SimpleSchema?

From Dev

Django REST Framework a boolean/None value in the request is getting set to False

From Dev

Django REST Framework a boolean/None value in the request is getting set to False

From Dev

How to deal with value objects in Entity Framework?

From Dev

How to Order By Dynamic value via Entity Framework?

From Dev

How to handle null value in Entity Framework?

From Dev

How to update all rows of a table with an aggregate value referenced by foreign key on an another table in Entity Framework

From Dev

How to save an identity value of one table to foreign key of another table in entity framework?

From Dev

How can set a default value constraint with Entity Framework 6 Code First?

From Dev

How can i set value in a datagridview linkbutton column in Winform appl. using entity framework

From Dev

Override sql default value on insert using Entity Framework Core (7)

From Dev

ASP.NET core Entity Framework null value connection string

From Dev

How to add value to set from another set?

From Dev

Entity Framework Core - Cannot insert explicit value for identity column in table 'EntryType' when IDENTITY_INSERT is set to OFF

From Dev

Project boolean value in aggregation framework

From Dev

Entity Framework Cannot insert the value NULL into column Identity Specification set to No

From Dev

Cannot set bool value to false in entity framework update method

From Dev

SQL how create a Boolean column based on the value of another column

From Dev

How to print a boolean value?

From Dev

How to print a boolean value?

From Dev

Using Entity Framework 6 Code First can I specify a default value for a Boolean field?

Related Related

  1. 1

    Error when set boolean value in entity

  2. 2

    How is this boolean value being set

  3. 3

    How to set null value to an existing attribute on Entity Framework

  4. 4

    argparse: Defaut value for nargs='*'?

  5. 5

    Spring @Scheduled defaut value

  6. 6

    How to set Entity Framework Core migration timeout?

  7. 7

    How to set default boolean value in JPA

  8. 8

    how to set @modelattribute boolean value inside html

  9. 9

    How to set false as a value using Boolean in SimpleSchema?

  10. 10

    Django REST Framework a boolean/None value in the request is getting set to False

  11. 11

    Django REST Framework a boolean/None value in the request is getting set to False

  12. 12

    How to deal with value objects in Entity Framework?

  13. 13

    How to Order By Dynamic value via Entity Framework?

  14. 14

    How to handle null value in Entity Framework?

  15. 15

    How to update all rows of a table with an aggregate value referenced by foreign key on an another table in Entity Framework

  16. 16

    How to save an identity value of one table to foreign key of another table in entity framework?

  17. 17

    How can set a default value constraint with Entity Framework 6 Code First?

  18. 18

    How can i set value in a datagridview linkbutton column in Winform appl. using entity framework

  19. 19

    Override sql default value on insert using Entity Framework Core (7)

  20. 20

    ASP.NET core Entity Framework null value connection string

  21. 21

    How to add value to set from another set?

  22. 22

    Entity Framework Core - Cannot insert explicit value for identity column in table 'EntryType' when IDENTITY_INSERT is set to OFF

  23. 23

    Project boolean value in aggregation framework

  24. 24

    Entity Framework Cannot insert the value NULL into column Identity Specification set to No

  25. 25

    Cannot set bool value to false in entity framework update method

  26. 26

    SQL how create a Boolean column based on the value of another column

  27. 27

    How to print a boolean value?

  28. 28

    How to print a boolean value?

  29. 29

    Using Entity Framework 6 Code First can I specify a default value for a Boolean field?

HotTag

Archive