Overriding code-generated DbContext constructor

前端 未结 5 1244
花落未央
花落未央 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:15

    i changed the context.tt as follows:

    <#=Accessibility.ForType(container)#> partial class <#=code.Escape(container)#> : DbContext
    {
        public <#=code.Escape(container)#>()
            : base("name=<#=container.Name#>")
        {
    
    <#
    if (!loader.IsLazyLoadingEnabled(container))
    {
    #>
            this.Configuration.LazyLoadingEnabled = false;
    <#
    }
    foreach (var entitySet in container.BaseEntitySets.OfType())
    {
        // Note: the DbSet members are defined below such that the getter and
        // setter always have the same accessibility as the DbSet definition
        if (Accessibility.ForReadOnlyProperty(entitySet) != "public")
        {
    #>
            <#=codeStringGenerator.DbSetInitializer(entitySet)#>
    <#
        }
    }
    #>
    var Method = (typeof(Entities)).GetMethods(System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).FirstOrDefault(x => x.Name == "OnModelConstructed");
    if (Method!=null) Method.Invoke(this,null);
        }
    

    so i can declare a OnModelConstructed method in a partial class of the context.

提交回复
热议问题