-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Insert default value for Shadow Property #13462
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Comments
@jasonycw |
@divega to find dupe |
Duplicate of #11001 |
@jasonycw It was pointed out that this can actually be done today with a value generator. For example: public class ConstantValueGenerator : ValueGenerator<string>
{
public override string Next(EntityEntry _) => "some_out_of_domain_data";
public override bool GeneratesTemporaryValues => false;
} modelBuilder
.Entity<Blog>()
.Property<string>("some_out_of_domain_column")
.HasValueGenerator<ConstantValueGenerator>(); This will set the value for the shadow property on every new entity before it is inserted. |
@ajcvickers Thanks, this is a good idea to group the default values in one place. One more similar issue we want to report too is about HasDefaultValueSql or HasComputedColumnSql m.Property("_email")
.HasColumnName("email");
m.Property<string>("another_email_column")
.HasDefaultValueSql("[email]");
m.Property<string>("is_gmail")
.HasComputedColumnSql("IIF([email] LIKE '%@gmail.com', 1, 0)"); The Any suggestion on how to map shadow property to be the same value as another column or do some logic? |
@jasonycw Overriding SaveChanges is probably your best bet for that at the current time. |
Insert default value with shadow property not working as expected
Steps to reproduce
Was hoping to use HasDefaultValue to insert some default value to db without adding it to our entity.
But it seems not working and we need to set the current value before save.
Here is some sample code
Further technical details
EF Core version: 2.1.4
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating system: Windows 10
IDE: Visual Studio 2017 15.8.5
The text was updated successfully, but these errors were encountered: