dotnet / dotnet/EntityFramework.Docs
Guidance on dealing with optional dependents with table splitting
- Dominant language
- Mermaid
- Stars
- 1.7k
- Forks
- 2k
- Avg merge
- 7d 23h
- Merged PRs (30d)
- 16
Description
After upgrading to EF Core 3.0, I can't update properties on an owned entity.
### Steps to reproduce
Start with this sample app that uses EF Core 2.2.6:
```xml
Exe
netcoreapp3.0
```
```c#
using Microsoft.EntityFrameworkCore;
using System;
using System.Linq;
namespace ConsoleApp3
{
public sealed class Entity
{
public int Id { get; set; }
public UserAction Created { get; private set; } = new UserAction();
public UserAction Updated { get; private set; } = new UserAction();
}
[Owned]
public sealed class UserAction
{
public DateTime? TimeUtc { get; set; }
public int? UserId { get; set; }
public string UserEmail { get; set; }
}
public sealed class ApplicationDbContext : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
base.OnConfiguring(optionsBuilder);
optionsBuilder.UseSqlServer("Server=(localdb)\\mssqllocaldb;Database=aspnet-WebApplication-6E483A89-BEE6-4A76-96CC-CEB276E5E112;Trusted_Connection=True;MultipleActiveResultSets=true");
}
public DbSet Entities { get; set; }
}
public static class Program
{
public static void Main()
{
using (var context = new ApplicationDbContext())
{
context.Database.EnsureDeleted();
context.Database.EnsureCreated();
context.Entities.Add(new Entity { Created = { TimeUtc = DateTime.UtcNow } });
context.SaveChanges();
}
using (var context = new ApplicationDbContext())
{
var entity = context.Entities.First();
entity.Updated.TimeUtc = DateTime.UtcNow;
context.Entities.Update(entity);
context.SaveChanges();
}
}
}
}
```
I always initialize `UserAction` properties in the class itself to make sure they're never null, even right after creating the entity.
After running this, as expected, `Updated_TimeUtc` will be set in the table row. But if I change the `Microsoft.EntityFrameworkCore.SqlServer` package version to `3.0.0` and run this again, `Updated_TimeUtc` will always be null. The call to `SaveChanges` doesn't execute any SQL. This bug is also present in `3.1.0-preview2.19525.5`.
I can't find any workaround other than manually executing some SQL to update the row. I tried calling `context.Update(entity.Updated)` or setting `context.Entry(entity.Updated).State = EntityState.Modified` but they throw exceptions.
### Further technical details
EF Core version: 3.0.0
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET Core 3.0
Operating system: Windows 10 Version 1903
IDE: Visual Studio 16.4.0 Preview 2.0
Contributor guide
Assessment
This issue has not been assessed yet.