You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Setting default value to enum property leads to getting that value assigned to that property after calling SaveChanges whether or not it's being set
#20645
Closed
art-grig opened this issue
Apr 15, 2020
· 2 comments
When setting default value to enum in model through builder in OnModelCreating it appears that after adding newly created entity with that enum to dbContext and calling SaveChanges regardless to whether enum has been set in the process of entity creation the corresponding field of the entity is getting set to the default value.
publicclassProgram{publicstaticvoidMain(){using(varcontext=newEntityContext()){context.Database.EnsureCreated();varorderType=OrderType.Sales;varorderDate=newDateTime(2,2,2);varorder=newOrder{Type=orderType,Date=orderDate};context.Add(order);context.SaveChanges();Console.WriteLine($"Order type should be {orderType} but it's {order.Type}");Console.WriteLine($"Order date should be {orderDate} and it's {order.Date}");}}}publicclassEntityContext:DbContext{protectedoverridevoidOnConfiguring(DbContextOptionsBuilderbuilder){varconnectionString=FiddleHelper.GetConnectionStringSqlServer();//Console.WriteLine($"Configuring EntityContext with ad-hoc database from Fiddle: {connectionString}");builder.UseSqlServer(newSqlConnection(connectionString)).EnableDetailedErrors().EnableSensitiveDataLogging();base.OnConfiguring(builder);}protectedoverridevoidOnModelCreating(ModelBuilderbuilder){builder.Entity<Order>().Property(o =>o.Date).HasDefaultValue(newDateTime(3,3,3));builder.Entity<Order>().Property(o =>o.Type).HasDefaultValue(OrderType.Purchase);}publicDbSet<Order>Orders{get;set;}}publicclassOrder{publicintId{get;set;}publicstringDescription{get;set;}publicdecimalAmount{get;set;}publicDateTimeDate{get;set;}publicOrderTypeType{get;set;}}publicenumOrderType{Sales,Purchase}
Further technical details
EF Core version: 3.1.3
Database provider: (e.g. Microsoft.EntityFrameworkCore.SqlServer)
Target framework: .NET Core 3.1
Operating system:
IDE: Visual Studio 2019 16.4.5
The text was updated successfully, but these errors were encountered:
For some reason steps to reproduce section with the code doesn't appear, however I've added the code there, not sure what's the reason, but I also include dotnetfiddle link with code for reproduction
art-grig
changed the title
Setting default value to enum leads to getting that value assigned to the entity property after calling SaveChanges whether or not it's being set
Setting default value to enum field leads to getting that value assigned to the entity property after calling SaveChanges whether or not it's being set
Apr 15, 2020
art-grig
changed the title
Setting default value to enum field leads to getting that value assigned to the entity property after calling SaveChanges whether or not it's being set
Setting default value to enum property leads to getting that value assigned to that property after calling SaveChanges whether or not it's being set
Apr 15, 2020
When setting default value to enum in model through builder in OnModelCreating it appears that after adding newly created entity with that enum to dbContext and calling SaveChanges regardless to whether enum has been set in the process of entity creation the corresponding field of the entity is getting set to the default value.
Here is dotnetfiddle link for reproduction: https://dotnetfiddle.net/oR13Ox
Steps to reproduce
Further technical details
EF Core version: 3.1.3
Database provider: (e.g. Microsoft.EntityFrameworkCore.SqlServer)
Target framework: .NET Core 3.1
Operating system:
IDE: Visual Studio 2019 16.4.5
The text was updated successfully, but these errors were encountered: