Mik*_*keW 4 .net c# t4 entity-framework
我有一个修改过的T4模板,它可以从我的edmx构建类,并且除了派生类之外它还能顺利运行.
Product : BaseItem // works fine as do all top level classes
TranslatedProduct : Product : BaseItem // dang
Run Code Online (Sandbox Code Playgroud)
我对如何以及在何处有条件地将T4模板设置为忽略感到困惑:在派生类的情况下BaseItem - 即
TranslatedProduct : Product
Run Code Online (Sandbox Code Playgroud)
例如:
<#=Accessibility.ForType(entity)#> <#=code.SpaceAfter(code.AbstractOption(entity))#>partial class <#=code.Escape(entity)#><#=code.StringBefore(" : ", code.Escape(entity.BaseType))#> : BaseItem
Run Code Online (Sandbox Code Playgroud)
在我的脑海里,我想象它 -
if(code.Escape(entity.BaseType).Equals(string.empty)
{
<#=Accessibility.ForType(entity)#> <#=code.SpaceAfter(code.AbstractOption(entity))#>partial class <#=code.Escape(entity)#> : BaseItem
}
else
{
<#=Accessibility.ForType(entity)#> <#=code.SpaceAfter(code.AbstractOption(entity))#>partial class <#=code.Escape(entity)#><#=code.StringBefore(" : ", code.Escape(entity.BaseType))#>
}
Run Code Online (Sandbox Code Playgroud)
但是我收到了语法错误,所以我想看看是否还有其他人试过这个,如果我走的是正确的道路
Mer*_*ham 11
您提供硬编码的脚本: BaseItem始终显示.这似乎破了.
原始代码是:
<#=Accessibility.ForType(entity)#> <#=code.SpaceAfter(code.AbstractOption(entity))#>partial class <#=code.Escape(entity)#><#=code.StringBefore(" : ", code.Escape(entity.BaseType))#>
Run Code Online (Sandbox Code Playgroud)
这使用了以下定义的类:
C:\ Program Files(x86)\ Microsoft Visual Studio 10.0\Common7\IDE\Extensions\Microsoft\Entity Framework Tools\Templates\Includes\EF.Utility.CS.ttinclude
<#= #>标记之间的脚本部分只是C#表达式,这些表达式返回的字符串是内联插入的.
该code.Escape方法将返回类型名称(作为字符串)或空字符串.
在code.StringBefore将附加第一串(" : ")的第二串(基本类型名称)之前,但仅当所述第二字符串是不空.
要做你想要完成的事情,你可以使用他们使用的相同技巧,但相反.不幸的是,你不能使用他们现有的类,因为他们没有某种AppendIfNotDefined方法.所以我们只会使用更复杂的表达式.
代替:
code.StringBefore(" : ", code.Escape(entity.BaseType))
Run Code Online (Sandbox Code Playgroud)
我们写道:
code.StringBefore(" : ",
string.IsNullOrEmpty(code.Escape(entity.BaseType))
? "BaseItem"
: code.Escape(entity.BaseType)
)
Run Code Online (Sandbox Code Playgroud)
这是整条线,全都塞满了:
<#=Accessibility.ForType(entity)#> <#=code.SpaceAfter(code.AbstractOption(entity))#>partial class <#=code.Escape(entity)#><#=code.StringBefore(" : ", string.IsNullOrEmpty(code.Escape(entity.BaseType)) ? "BaseItem" : code.Escape(entity.BaseType))#>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2343 次 |
| 最近记录: |