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

Warn when no service to replace #9947

Open
Tracked by #22961
JonPSmith opened this issue Oct 3, 2017 · 3 comments
Open
Tracked by #22961

Warn when no service to replace #9947

JonPSmith opened this issue Oct 3, 2017 · 3 comments

Comments

@JonPSmith
Copy link

JonPSmith commented Oct 3, 2017

I'm trying to create an example of replacing a service inside EF Core as shown in a Channel 9 video by Rowan Miller starting at 45 minutes in.

My example (see below) doesn't work. And I can confirm that the constructor is not called, nor the overriden method are called, as my break points are never triggered. Can you tell me what I am doing wrong.

Steps to reproduce

Here is one of the versions I have tried (I have tried a few permutations and none work).

Here is the unit test, with the new CustomSqlServerTypeMapper

public class CustomSqlServerTypeMapper : SqlServerTypeMapper
{
    public CustomSqlServerTypeMapper(RelationalTypeMapperDependencies dependencies) 
        : base(dependencies) {}

    public override RelationalTypeMapping FindMapping(IProperty property)
    {
        var currentMapping = base.FindMapping(property);
        if (property.ClrType == typeof(string) && property.Name.EndsWith("Ascii"))
            return new StringTypeMapping(currentMapping.StoreType, 
                currentMapping.DbType, true, currentMapping.Size);

        return currentMapping;
    }

    public override RelationalTypeMapping FindMapping(string storeType)
    {
        return base.FindMapping(storeType);
    }
}

[Fact]
public void ReplaceTypeMapperOk()
{
    //SETUP
    var connectionString = AppConstants.ConnectionString;
    var optionsBuilder = new DbContextOptionsBuilder<TestDbContext>();
    optionsBuilder.UseSqlServer(connectionString);
    optionsBuilder.ReplaceService<SqlServerTypeMapper, CustomSqlServerTypeMapper>();

    using (var context = new TestDbContext(optionsBuilder.Options))
    {
        context.Database.EnsureDeleted();
        context.Database.EnsureCreated();

        //ATTEMPT 
        var entity = context.Model.FindEntityType(typeof(ScalarEntity));
        var mapInfo1 = entity.GetProperty(nameof(ScalarEntity.StringAscii));

        //VERIFY
        mapInfo1.ClrType.ShouldEqual(typeof(string));
        mapInfo1.IsUnicode().ShouldEqual(false);

    }
}

My entity class is

public class ScalarEntity
{
    public int Id { get; set; }

    public string StringMax { get; set; }

    [Required]
    [MaxLength(20)]
    public string String20 { get; set; }

    public string StringAscii { get; set; }
}

Further technical details

EF Core version: 2.0.0
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating system: Windows 10
IDE: VS2017 15.3

@ajcvickers
Copy link
Member

@JonPSmith Services are registered via their contract interfaces, which in this case is IRelationalTypeMapper. So you would need to do this:

optionsBuilder.ReplaceService<IRelationalTypeMapper, CustomSqlServerTypeMapper>();

Note for triage: we should probably warn if no service is found to be replace.

@JonPSmith
Copy link
Author

Thanks @ajcvickers. This fixed it. Note: The video of 1.1 required the SqlServerTypeMapper as the first type, so this is a change in 2.0.0.

@ajcvickers ajcvickers reopened this Oct 3, 2017
@ajcvickers
Copy link
Member

Putting this on the backlog and up-for-grabs to warn in this case, but it's low priority since service replacement is an advanced feature anyway.

@ajcvickers ajcvickers added this to the Backlog milestone Oct 4, 2017
@AndriySvyryd AndriySvyryd changed the title Question: How to replace a service in EF 2.0.0 Warn when no service to replace Oct 19, 2022
@ajcvickers ajcvickers removed their assignment Aug 31, 2024
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

3 participants