-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Computed Column being used in update query on savechanges #19176
Comments
@Abcdma I have not been able to reproduce this--see my code below. Please post a small, runnable project or complete code listing like below that demonstrates the behavior you are seeing. public class Taxes
{
public int Id { get; set; }
public decimal Multiplier { get; set; }
public decimal Percentage { get; set; }
public DateTime LastModified { get; set; }
public bool IsDeleted { get; set; }
}
public class BloggingContext : DbContext
{
private readonly ILoggerFactory Logger
= LoggerFactory.Create(c => c.AddConsole());
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Taxes>(b =>
{
b.Property(v => v.Multiplier)
.HasColumnType("decimal(3,2)")
.HasColumnName("Multiplier")
.HasComputedColumnSql(
"(CONVERT([decimal](3,2),CONVERT([decimal](3,0),[Percentage])/(100.00)+(1))) PERSISTED");
});
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder
.UseLoggerFactory(Logger)
.EnableSensitiveDataLogging()
.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=Test;ConnectRetryCount=0");
public override int SaveChanges()
{
foreach (var entry in ChangeTracker.Entries()
.Where(e => e.Properties.Any(p => p.Metadata.Name == "IsDeleted") && e.State == EntityState.Deleted))
{
entry.State = EntityState.Modified;
entry.CurrentValues["IsDeleted"] = true;
}
return base.SaveChanges();
}
}
public class Program
{
public static async Task Main()
{
using (var context = new BloggingContext())
{
context.Database.EnsureDeleted();
context.Database.EnsureCreated();
context.Add(new Taxes
{
Percentage = 10.0m,
LastModified = DateTime.Now
});
context.SaveChanges();
}
using (var context = new BloggingContext())
{
var taxes = context.Set<Taxes>().Single();
context.Remove(taxes);
context.SaveChanges();
}
}
} Log
|
Note for triage: The key thing here appears to be that the mapping to a computed column is in an owned entity. /cc @AndriySvyryd |
I did a cleanup in the entire code to remove all the unnecessary stuff. |
This isssue is occuring with SQL Server IdentityColumn as well. |
There is another problem with the OwnedEntities not configuring the Required, in the same project I posted above this configuration
Is generating a nullable column Already saw that it's a known thing #12100 |
I'm still seeing this exception in 5.0, but only when a table has multiple instances of the same owned type, each with a computed property. |
@lakeman Please open a new issue with a small repro project or code snippet |
I have a computed column in a OwnedEntity (it's being mapped using the following implementation #15681 (comment))
If I change for example the percentage everything works fine and it updates as expected. The problem is, when I delete I want a soft delete so in savechanges I execute the following
EF is generating the following (removed a few fields for brevity)
The Multiplier column should have been ignored.
Stack trace
EF Core 3.1
Best regards.
The text was updated successfully, but these errors were encountered: