Skip to content

Custom Migration Operations shows incorrect way to get type mapping source for string #4768

Closed

Description

@klemmchr

Type of issue

Code doesn't work

Description

Using a MigrationOperation shows the following code

public class MyMigrationsSqlGenerator : SqlServerMigrationsSqlGenerator
{
    public MyMigrationsSqlGenerator(
        MigrationsSqlGeneratorDependencies dependencies,
        ICommandBatchPreparer commandBatchPreparer)
        : base(dependencies, commandBatchPreparer)
    {
    }

    protected override void Generate(
        MigrationOperation operation,
        IModel model,
        MigrationCommandListBuilder builder)
    {
        if (operation is CreateUserOperation createUserOperation)
        {
            Generate(createUserOperation, builder);
        }
        else
        {
            base.Generate(operation, model, builder);
        }
    }

    private void Generate(
        CreateUserOperation operation,
        MigrationCommandListBuilder builder)
    {
        var sqlHelper = Dependencies.SqlGenerationHelper;
        var stringMapping = Dependencies.TypeMappingSource.FindMapping(typeof(string)); // <--------------------------

        builder
            .Append("CREATE USER ")
            .Append(sqlHelper.DelimitIdentifier(operation.Name))
            .Append(" WITH PASSWORD = ")
            .Append(stringMapping.GenerateSqlLiteral(operation.Password))
            .AppendLine(sqlHelper.StatementTerminator)
            .EndCommand();
    }
}

The marked line returns null.

The CLR Type mappings simply don't contain a string type key
image

I am unable to find the right way of getting a string type mapping.

Page URL

https://learn.microsoft.com/en-us/ef/core/managing-schemas/migrations/operations

Content source URL

https://github.com/dotnet/EntityFramework.Docs/blob/main/entity-framework/core/managing-schemas/migrations/operations.md

Document Version Independent Id

aafc9673-c635-3cf2-6235-8577ff15d19c

Article author

@bricelam

Activity

  1. added a commit that references this issue on Aug 11, 2026
    bb42937
  2. added this to the 11.0.0 milestone on Aug 13, 2026
  3. added theissue type on Aug 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions