efcore icon indicating copy to clipboard operation
efcore copied to clipboard

Cannot add row version to owned entity

Open ItsSmiffy opened this issue 6 years ago • 6 comments

I've upgraded to EFCore 3.1, but while working through the collection of breaking changes i can't seem to rectify the change 'All entities sharing a table with a concurrency token column have to map it to a property'.

public class ParentEntity : Entity {
     public byte[] RowVersion { get; protected set; }
}

public class SubParentEntity : ParentEntity {
     public OwnedEntity OwnedEntity { get; protected set; }
}

public class OwnedEntity : ValueObject {
     public string OwnedEntityProperty { get; protected set; }
}

// parent entity mapping
builder.Property(x => x.RowVersion).IsRowVersion();

// subparent entity mapping
builder.ownsOne(x => x.OwnedEntity, cfg => {
     cfg.Property(x => x.OwnedEntityProperty).IsRequired();
     cfg.Property<byte[]>("RowVersion").IsRowVersion().HasColumnName("RowVersion");
});

I have a hierarchy of classes, with some of the subclasses having owned entities. I've mapped a shadow property on the owned entity to the same RowVersion column for each subclass which has an owned entity but still get an invalid operation exception:

Entity type 'OwnedEntity' doesn't contain a property mapped to the store-generated concurrency token column 'ParentEntity_RowVersion' that is used by another entity type sharing the table 'ParentEntity '. Add a store-generated property mapped to the same column to 'OwnedEntity'. It can be in shadow state.

EF Core version: 3.1 Database provider: Pomelo 3.1

ItsSmiffy avatar Dec 19 '19 13:12 ItsSmiffy

I'm encountering a similar problem with an owned entity on a derived type complaining about the concurrency token. I get the same error when adding a shadow property to the owned entity. @ItsSmiffy was there something you did to fix the problem?

Meyce avatar Feb 28 '20 16:02 Meyce

See https://docs.microsoft.com/en-us/ef/core/modeling/table-splitting#concurrency-tokens

AndriySvyryd avatar Mar 02 '20 06:03 AndriySvyryd

@AndriySvyryd, Thanks I came across that document. What was unclear(at least to me) was all the places I needed it. I am using TPH so I've got a abstract base and 3 derived types, with a couple of owned entities all on the same table. I found I needed the shadow property on the base, all the derived types, and the owned entities to get it to work. I had assumed that the derived types wouldn't need the shadow property as it was already on the base.

Thanks.

Meyce avatar Mar 02 '20 14:03 Meyce

@Meyce can you paste code how you made it work? I'm trying to add this to every single place: base, derived types and owned entites, and nothing works.

E.g.:

builder.OwnsOne<Bounds>(x => x.Bounds, b =>
            {
                b.Property(p => p.CategoryIds).HasColumnName("category_ids");
                b.Property(p => p.ProductIds).HasColumnName("product_ids");
                b.Property<uint>("xmin");
            });
builder.OwnsOne<Bounds>(x => x.Bounds, b =>
            {
                b.Property(p => p.CategoryIds).HasColumnName("category_ids");
                b.Property(p => p.ProductIds).HasColumnName("product_ids");
                b.Property<uint>("xmin").HasColumnName("xmin").IsConcurrencyToken();
            });
builder.OwnsOne<Bounds>(x => x.Bounds, b =>
            {
                b.Property(p => p.CategoryIds).HasColumnName("category_ids");
                b.Property(p => p.ProductIds).HasColumnName("product_ids");
                b.Property<uint>("xmin").HasColumnType("xid").IsConcurrencyToken();
            });
            {
                b.Property(p => p.CategoryIds).HasColumnName("category_ids");
                b.Property(p => p.ProductIds).HasColumnName("product_ids");
                b.Property("xmin").IsConcurrencyToken();
            });

mpszczolinski avatar Dec 27 '20 13:12 mpszczolinski

Has something changed, or is this Npgsql.EntityFrameworkCore.PostgreSQL issue? I tried to follow the instructions to define the shadow concurrency token property on the base, all derived, and the owned by them entity types. I get this when trying to make a migration:

Unable to create a 'DbContext' of type 'Context'. The exception ''Entry_Foo.Bar#Bar._TableSharingConcurrencyTokenConvention_RowVersion' and 'Entry_Foo.Bar#Bar._TableSharingConcurrencyTokenConvention_RowVersion1' are both mapped to column 'xmin' in 'entity_data', but the properties are contained within the same hierarchy. All properties on an entity type must be mapped to unique different columns.' was thrown while attempting to create an instance.

which suggests that this is actually invalid? Entry_Foo is owned by an Entity-derived entity type, with TPH in use.

ZzZombo avatar Nov 27 '24 04:11 ZzZombo

@ZzZombo I don't think anything has changed, but this is a 4-year-old issue. Please open a new issue with a minimal repro.

roji avatar Nov 27 '24 07:11 roji