-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Owned types in table per hierarchy cause duplicate rowversion column in query that then throws system exception #27888
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
Milestone
Comments
Notes for triage: Query expression:
Model:
Code: #nullable enable
public static class Your
{
public static string ConnectionString = @"Data Source=(LocalDb)\MSSQLLocalDB;Database=SixOh";
}
public abstract class Account
{
public Guid Id { get; set; }
public ulong _etag { get; set; }
public Account(Guid id, ulong etag)
{
Id = id;
_etag = etag;
}
protected Account() { }
}
public class Person : Account
{
public FullName FullName { get; set; }
public Person(Guid id, FullName fullName, ulong etag) : base(id, etag)
{
FullName = fullName;
}
protected Person() { }
}
public class FullName
{
public string FirstName { get; set; }
public string LastName { get; set; }
public FullName(string firstName, string lastName)
{
FirstName = firstName;
LastName = lastName;
}
}
public class AccountConfiguration : IEntityTypeConfiguration<Account>
{
public void Configure(EntityTypeBuilder<Account> entity)
{
entity.ToTable("Account", "AC");
entity.HasDiscriminator<string>("Type");
entity.Property(e => e._etag).IsRowVersion().HasConversion<byte[]>();
}
}
public class PersonConfiguration : IEntityTypeConfiguration<Person>
{
public void Configure(EntityTypeBuilder<Person> entity)
{
entity.ToTable("Account", "AC");
entity.OwnsOne(e => e.FullName, e =>
{
e.Property(e => e.FirstName).HasColumnName("FirstName");
e.Property(e => e.LastName).HasColumnName("LastName");
});
}
}
public class SomeDbContext : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder
.UseSqlServer(Your.ConnectionString)
.LogTo(Console.WriteLine, LogLevel.Debug)
.EnableSensitiveDataLogging();
public DbSet<Account> Accounts { get; set; }
public DbSet<Person> People { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
new PersonConfiguration().Configure(modelBuilder.Entity<Person>());
new AccountConfiguration().Configure(modelBuilder.Entity<Account>());
}
}
public class Program
{
public static void Main()
{
Guid accountId = new Guid("da19e7f2-2520-48fd-9a52-d90c5e2d1e6b");
using (var context = new SomeDbContext())
{
context.Database.EnsureDeleted();
context.Database.EnsureCreated();
context.Add(new Person(accountId, new FullName("A", "V"), 0l));
context.SaveChanges();
}
using (var context = new SomeDbContext())
{
var account = context.Accounts.SingleOrDefault(e => e.Id == accountId);
}
}
} |
Could be related to #22584 |
My colleague found a workaround. You need to define the etag property in every OwnsType hat is in the entity. So it seems to be related to #22584
|
# for free
to join this conversation on GitHub.
Already have an account?
# to comment
We ran into a major issue using table per hierarchy. The base class has a
RowVersion
attribute. When the table is queried the created query includes theRowVersion
column twice and crashes withThis only happens when a derived class has an owned type and therefore no converter!
Here is an example:
LINQ
Created Query: [a].[_etag] is doubled in select
Classes
Configuration
Include provider and version information
EF Core version: 6.04
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 6.0
Operating system: Win 10
IDE: Visual Studio 2022 17.1.2
The text was updated successfully, but these errors were encountered: