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


i got quite same problem in question : how override sql server default value constraint on boolean when inserting new entity? [closed]

like him, value of boolean client controller, false, it's set true call of _context.savechanges(); because of entity framework , default value constraint in database.

but : i'm using entity framework core , don't have [databasegenerated(databasegeneratedoption.computed)] annotation remove fix problem.

in 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 database :

create table myentity(     id      integer identity(1,1) not null,     ...     active  bit not null constraint df_myentity_active default 1 ); 

in 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 _context.savechanges(); in controller         // here myentity.active = true     }     ... } 

it seems ef doesn't map c# boolean sql bit correctly , takes default value. have issue force false value ?

finally can have data annotations generated in ef model using --data-annotations option in ef command. put [databasegenerated(databasegeneratedoption.none)] on property , don't use default contraint of database.


Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -