dotnet / efcore

EF Core is a modern object-database mapper for .NET. It supports LINQ queries, change tracking, updates, and schema migrations.
https://docs.microsoft.com/ef/
MIT License
13.49k stars 3.13k forks source link

SQL Server Primary key/Unique contrainst index with options DATA_COMPRESSION #33145

Open deadcativan opened 4 months ago

deadcativan commented 4 months ago

In #30408 index options "DATA_COMPRESSION" is supported, I think there is another place where the same thing is needed. It's the primary key when creating a table for an entity.

Currently I create a custom class extends SqlServerAnnotationProvider to add annotation for data compression as following:

    public class ExtendedSqlServerAnnotationProvider : SqlServerAnnotationProvider
    {
        public ExtendedSqlServerAnnotationProvider(RelationalAnnotationProviderDependencies dependencies) : base(dependencies)
        {
        }

        public override IEnumerable<IAnnotation> For(IUniqueConstraint constraint, bool designTime)
        {
            foreach (var annotation in base.For(constraint, designTime))
            {
                yield return annotation;
            }

            if (!designTime)
            {
                yield break;
            }

            var key = constraint.MappedKeys.First();

            var table = constraint.Table;

            var dataCompression = key.GetDataCompression(StoreObjectIdentifier.Table(table.Name, table.Schema));
            if(dataCompression.HasValue)
            {
                yield return new Annotation(SqlServerAnnotationNames.DataCompression, dataCompression.Value);
            }

        }
    }

And then I extend SqlServerMigrationsSqlGenerator to build SQL for index options according to this annotation in AddPrimaryKeyOperation and AddUniqueConstraintOperation.

Finally I add extension methods xxx.UseDataCompression like the way index does.

roji commented 4 months ago

Makes sense - @hmajerus are you interested in submitting a PR for this, as you did the original PR for the index case (#30408)?

hmajerus commented 4 months ago

Makes sense - @hmajerus are you interested in submitting a PR for this, as you did the original PR for the index case (#30408)?

Yeah, I can look into it later next week.

hmajerus commented 4 months ago

So, did not get to this last week. Fairly busy lately and for the next month so won't get to this anytime soon, but think I could do it for the EF9 release. If someone else wants to do it before then that's fine with me.