Overriding code-generated DbContext constructor

前端 未结 5 1246
花落未央
花落未央 2021-01-11 14:57

I\'m sure I\'ve done this before at some stage, but I can\'t figure out how to now! My scenario:

// This is generated from EDMX
public partial class HOLDbEnt         


        
5条回答
  •  再見小時候
    2021-01-11 15:16

    I used the string interpolations feature of C# 6 in the .tt code generation file.
    The generated code becomes

    public MyEntities()
                : base($"name={MyConfigurationManager.ConnectionStringKey("MyEntities")}")
    {
    }
    

    when you use

    public <#=code.Escape(container)#>()
        : base($"name={MyConfigurationManager.ConnectionStringKey("<#=container.Name#>")}")
    

    in the .tt file.

    public static string ConnectionStringKey(string key) in static class MyConfigurationManager in my case adds the initials of the login to the key and checks whether any connection string in ConfigurationManager.ConnectionStrings has that key in which case that key is returned and otherwise just returns the default key.

    So now the connectionstring may be different per-user.
    E.g.

    
    
    

    means that user F.B. uses the latter key while all others the former key.

提交回复
热议问题