Entity Framework does not include columns with a default value when pasted into a query

I have a model that has several columns set with default values, e.g.

table.Column<bool>(nullable: false, defaultValueSql: "1")

When I save a new object to the database using context.SaveChanges(), I noticed that the columns with the default values ​​are not included in the insert in the query that the Entity Framework generates, so the values ​​generated in the database are the default values ​​instead of the ones that I I pass in the model.

Do I have to set any property in context to be able to set these properties using code? I use EF Core, but I do not know if this is the common behavior of all versions of EF.

UPDATE: the code is pretty simple. This is the pseudo code of what I have. A model has some properties defined with restrictions, such as those described above. table.Column<bool>(nullable: false, defaultValueSql: "1")

As an example, I will use the MyBooleanProperty column. I have a service:

var newModel = new GEAddress();
newModel = someEntity.MyBooleanProperty; //it is false,but ends up as 1 in the database

I use Unit of Work and Repositories, so I have

_unitOfWork.MyModelRepository.Add(newModel);
_unitOfWork.SaveChanges();

In the VS output window, I see how it sends all the properties in the request INSERT INTO, and then it matters SELECTto properties with default values. The result is newModel in the database with all the values ​​I sent, except for the columns with the default values. I cannot change the configuration for these tables because it is being used by another system that needs these rules.

, , . , , .

+4
2

.

, :

public class Test
{
    public int ID { get; set; }
    public int IntDef { get; set; }
    public bool BoolDef { get; set; }
    public DateTime? DateDef { get; set; }
}

( , DateTime NULL)

:

modelBuilder.Entity<Test>().HasKey(a => a.ID);
modelBuilder.Entity<Test>().Property(s => s.DateDef).HasDefaultValueSql("GETDATE()");
modelBuilder.Entity<Test>().Property(s => s.IntDef).HasDefaultValueSql("1");
modelBuilder.Entity<Test>().Property(s => s.BoolDef).HasDefaultValue(true);
// Equivalent:
// modelBuilder.Entity<Test>().Property(s => s.BoolDef).HasDefaultValueSql("1");

SQL, :

CREATE TABLE [Tests] (
    [ID] int NOT NULL IDENTITY,
    [BoolDef] bit NOT NULL DEFAULT 1,
    [DateDef] datetime2 DEFAULT (GETDATE()),
    [IntDef] int NOT NULL DEFAULT (1),
    CONSTRAINT [PK_Tests] PRIMARY KEY ([ID])
);

Test - , insert:

INSERT INTO [Tests]
DEFAULT VALUES;
SELECT [ID], [BoolDef], [DateDef], [IntDef]
FROM [Tests]
WHERE @@ROWCOUNT = 1 AND [ID] = scope_identity();

, ( ) . [, EF-Core. EF6 ( ) , DatabaseGeneratedOption.Computed.

Test:

ID IntDef BoolDef DateDef
1  1      True    21-11-16 19:52:56 

Test , , , , :

var item = new Test
{ 
    IntDef = default(int), // 0 
    BoolDef = default(bool), // false
    DateDef = default(DateTime), // 01-01-01 0:00:00
};

SQL:

exec sp_executesql N'SET NOCOUNT ON;
INSERT INTO [Tests] ([DateDef])
VALUES (@p0);
SELECT [ID], [BoolDef], [IntDef]
FROM [Tests]
WHERE @@ROWCOUNT = 1 AND [ID] = scope_identity();
',N'@p0 datetime2(7)',@p0='0001-01-01 00:00:00'

, EF , . , , DateTime , . DateDef .

:

ID IntDef BoolDef DateDef
1  1      True    01-01-01 0:00:00 

, - !

:

EF-Core, .Net, , .Net (, false ).

, , , EF-Core .


, EF , ValueGeneratedNever(), :

modelBuilder.Entity<Test>().Property(s => s.IntDef)
            .HasDefaultValueSql("1").ValueGeneratedNever();

, EF . , , , nullable, .


EF Core 1.1.0.

+5

,

[DatabaseGenerated(DatabaseGeneratedOption.Computed)]  

ef insert .
EF SET UPDATE SET, .

, , , , .

+2

Source: https://habr.com/ru/post/1660847/


All Articles