Skip to content
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

[Migrations] EFCore doesn't respect IsRowVersion for properties with conversion to byte[] #19590

Closed
akamud opened this issue Jan 15, 2020 · 2 comments

Comments

@akamud
Copy link

akamud commented Jan 15, 2020

When using an entity with a property with a type different from byte[] that is configured as a RowVersion, the migration generated doesn't specify that the column should be a rowversion, and instead just creates a byte array column on SQL Server. This happens even if the property has a valid conversion to byte[] (like ulong).

Steps to reproduce

This is the simplest repro I could create. You can recreate the migration to verify the problem.

This entity:

public class Entity
{
    public int Id { get; set; }
    public ulong RowVersion { get; set; }
}

With this configuration:

modelBuilder.Entity<Entity>()
    .Property(x => x.RowVersion)
    .IsRowVersion()
    .HasConversion<byte[]>();

Will not generate a migration that indicates that the RowVersion column should be a rowversion from SQLServer.

This is the relevant part from the generated migration:

            migrationBuilder.CreateTable(
                name: "Entities",
                columns: table => new
                {
                    // notice the missing rowVersion: true
                    RowVersion = table.Column<byte[]>(nullable: false)
                }

If you change the property to be a byte[] in the C# mapped entity, it starts generating the migration correctly again.
I believe the problem is this check, since EF core corrctly understands the ulong to byte[] conversion and works perfectly when querying or in CRUD operations, I believe this check shouldn't ignore the IsRowVersion mapping info based on the assumption that only a property with a byte[] type is valid.

Properties with a valid conversion to byte[] should also be considered when setting the rowVersion flag.

Workaround

As a workaround, we are using the .ColumnType("rowversion"), but this is not valid in all scenarios, since if you decide to "fix" the issue making the property a byte[], EF Core won't understand something has changed, generating an empty migration. So if you apply a faulted migration, undoing the mistake is not as simple as generating a new migration from ef core.

Further technical details

EF Core version: 3.1.1. But confirmed to happend at least since 2.1.14
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET Core 3.1
Operating system: Windows and Mac
IDE: Visual Studio 2019 16.3.1

@ajcvickers
Copy link
Contributor

@akamud Thanks for the detailed issue; I think this is essentially a duplicate of #12434 but we will revisit the priority.

but this is not valid in all scenarios, since if you decide to "fix" the issue making the property a byte[], EF Core won't understand something has changed, generating an empty migration.

I don't think EF should be creating an empty migration here. Can you file a new issue for this?

@ajcvickers
Copy link
Contributor

From team discussion: Made #12434 a stretch goal for 5.0 and closing this as a duplicate.

@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants