How can I stop Entity Framework 5 migrations adding dbo. into key names?

前端 未结 4 1114
情深已故
情深已故 2020-12-05 00:32

I started a project using Entity Framework 4.3 Code First with manual migrations and SQL Express 2008 and recently updated to EF5 (in VS 2010) and noticed that now when I ch

4条回答
  •  鱼传尺愫
    2020-12-05 01:00

    You can customize the generated code by sub-classing the CSharpMigrationCodeGenerator class:

    class MyCodeGenerator : CSharpMigrationCodeGenerator
    {
        protected override void Generate(
            DropIndexOperation dropIndexOperation, IndentedTextWriter writer)
        {
            dropIndexOperation.Table = StripDbo(dropIndexOperation.Table);
    
            base.Generate(dropIndexOperation, writer);
        }
    
        // TODO: Override other Generate overloads that involve table names
    
        private string StripDbo(string table)
        {
            if (table.StartsWith("dbo."))
            {
                return table.Substring(4);
            }
    
            return table;
        }
    }
    

    Then set it in your migrations configuration class:

    class Configuration : DbMigrationsConfiguration
    {
        public Configuration()
        {
            CodeGenerator = new MyCodeGenerator();
        }
    }
    

提交回复
热议问题