C# 强制EF6脚手架工包括';使用';命名空间内的指令
生成数据库上下文类和POCO实体类时,使用Scaffold DbContext命令(DB first方法)以以下方式生成类:C# 强制EF6脚手架工包括';使用';命名空间内的指令,c#,.net,entity-framework,entity-framework-6,C#,.net,Entity Framework,Entity Framework 6,生成数据库上下文类和POCO实体类时,使用Scaffold DbContext命令(DB first方法)以以下方式生成类: using System; using System.Collections.Generic; namespace Database { public partial class User { public User() { ... } }
using System;
using System.Collections.Generic;
namespace Database
{
public partial class User
{
public User()
{ ... }
}
}
我想要实现的是在名称空间中使用“using”指令生成POCO类,如下所示:
namespace Database
{
using System;
using System.Collections.Generic;
public partial class User
{
public User()
{ ... }
}
}
是否可以在不修改EF T4模板的情况下实现?您可以编写一种方法,读取POCO文件的行,使用行检测
,删除它们并在命名空间
行下重新写入它们
这可以通过使用T4模板来实现。每次在构建时运行此模板,以确保修改所有POCO
单个模型的示例:
<#@ import namespace="System.IO" #>
<#@ template debug="false" hostspecific="true" language="C#" #>
<#
string path = this.Host.ResolvePath(@"PocoClass.cs");
var PocoLines= File.ReadLines(path).ToArray();
List<string> usingStatements = new List<string>();
foreach (var line in pocoLines)
{
if(Regex.Match(line, "RegexForUsings") //or use String.Contains("using");
usingStatements.Add(line);
}
//insert lines again in PocoLines
#>
您可以编写一种方法,读取POCO文件的行,使用
行检测,删除它们,然后在命名空间
行下重新写入它们
这可以通过使用T4模板来实现。每次在构建时运行此模板,以确保修改所有POCO
单个模型的示例:
<#@ import namespace="System.IO" #>
<#@ template debug="false" hostspecific="true" language="C#" #>
<#
string path = this.Host.ResolvePath(@"PocoClass.cs");
var PocoLines= File.ReadLines(path).ToArray();
List<string> usingStatements = new List<string>();
foreach (var line in pocoLines)
{
if(Regex.Match(line, "RegexForUsings") //or use String.Contains("using");
usingStatements.Add(line);
}
//insert lines again in PocoLines
#>