Warn when no service to replace

Open
#9,947 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
30/100
Issue type
Bug
Clarity
Needs clarification
Activity status
Stale
Tech stack
csharp
Domain
database

Research direction

Start with the ReplaceService<SqlServerTypeMapper, CustomSqlServerTypeMapper>() call and reproduce the behavior using the supplied ReplaceTypeMapperOk test on EF Core 2.0. Trace the service-replacement path to determine why the custom mapper is not invoked; done means the issue is diagnosed and the requested warning is covered by a regression test.

Written by the indexing model from the issue text.

Description

area-global

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

Dominant language
C#
Stars
14.8k
Forks
3.4k
Avg merge
2d 5h
Merged PRs (30d)
134

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from dotnet/efcore

All issues in dotnet/efcore

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.