我意识到这有点过时了,但我最近找到了一个在 Save 上转换 Edmx 的解决方案,我认为我会分享。请注意,我们使用的是 Visual Studio 2012、Entity Framework 6.0 和 .Net 4.5。我们没有使用 Code First。
我们的问题是通过实体框架生成的视图具有我们不想要的额外主键列(而不是没有我们需要的列)。我们无法在视图上创建唯一性约束,因为视图引用了非确定性函数。因此,这意味着正确获取 View 键列的唯一方法是更新 edmx 文件。
为此,我更新了 T4 模板以加载 edmx 文件,使用 xslt 对其进行翻译,然后再次保存。这意味着每次开发人员从设计器窗口保存 edmx 文件时,它都会在生成 .cs 类之前正确更新。作为一项附加检查,我还创建了一个 powershell 脚本来在我们的自动构建期间检查主键。
这是一些示例代码(在顶部附近的 Model1.tt 文件中)。
<#@ template language="C#" debug="false" hostspecific="true"#>
<#@ include file="EF.Utility.CS.ttinclude"#><#@ output extension=".cs"#>
<#@ assembly name="System.Xml" #>
<#@ import namespace="System.Xml" #>
<#@ import namespace="System.Xml.Xsl" #>
<#
XmlDocument rawXDoc = new XmlDocument();
XmlDocument xDoc = new XmlDocument();
XmlReaderSettings settings = new XmlReaderSettings {
//ConformanceLevel = ConformanceLevel.Document;
DtdProcessing = DtdProcessing.Prohibit
};
//Note that to use the Host.ResolvePath below you must set hostspecific="true" in the template directive.
using (FileStream rawDocFileSteam = File.OpenRead(Host.ResolvePath("MyDataModel.edmx"))) {
using (XmlReader rawDocReader = XmlReader.Create(rawDocFileSteam, settings)) {
using (XmlTextReader xsltReader = new XmlTextReader(Host.ResolvePath("DataModelTransform.xslt")) ) {
XslCompiledTransform xsltTransform = new XslCompiledTransform();
xsltTransform.Load(xsltReader); //Ensure the XML Resolver is null, or a XmlSecureResolver to prevent a Billion Laughs denial of service.
using (MemoryStream ms = new MemoryStream()) {
xsltTransform.Transform(rawDocReader, null, ms);
ms.Position = 0;
xDoc.Load(ms);
}
}
}
}
xDoc.Save(Host.ResolvePath("MyDataModel.edmx"));
#>
这是我们使用的 xslt 文件的示例。它做了两件事。1、在Transaction_Detail_Base的Version字段中增加ConcurrencyFixed;和 2. 删除无效的主键列。
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ssdl="http://schemas.microsoft.com/ado/2009/11/edm/ssdl"
xmlns:edmx="http://schemas.microsoft.com/ado/2009/11/edmx"
xmlns:edm="http://schemas.microsoft.com/ado/2009/11/edm"
xmlns:annotation="http://schemas.microsoft.com/ado/2009/02/edm/annotation"
>
<xsl:output method="xml" indent="yes"/>
<!--Ensure blank lines aren't left when we remove invalid PrimaryKey fields.-->
<xsl:strip-space elements="*"/>
<xsl:template match="@*|*|processing-instruction()|comment()">
<xsl:call-template name="CopyDetails"/>
</xsl:template>
<xsl:template name="CopyDetails">
<xsl:copy>
<xsl:apply-templates select="@*|*|text()|processing-instruction()|comment()"/>
</xsl:copy>
</xsl:template>
<!--Set concurrency mode to fixed for Transaction_Detail_Base.-->
<xsl:template match="edmx:ConceptualModels/edm:Schema/edm:EntityType[@Name='Transaction_Detail_Base']/edm:Property[@Name='Version']">
<xsl:call-template name="AddConcurrencyAttribute"/>
</xsl:template>
<!-- Add the ConcurrencyAttribute if it doesn't exist, otherwise update it if it does -->
<xsl:template name="AddConcurrencyAttribute">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
<xsl:attribute name="ConcurrencyMode">Fixed</xsl:attribute>
</xsl:copy>
</xsl:template>
<!-- Remove unused primary key columns from views. Should be removed from StorageMode and ConceptualModels -->
<!--Transaction_Detail. ssdl is the StorageModel section, edm is the ConceptualModel section-->
<xsl:template match="ssdl:EntityType[@Name='Transaction_Detail']/ssdl:Key/ssdl:PropertyRef | edm:EntityType[@Name='Transaction_Detail']/edm:Key/edm:PropertyRef">
<xsl:if test="@Name='Asset' or @Name='Date' or @Name='Portfolio' or @Name='System_Reference'">
<xsl:call-template name="CopyDetails"/>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
最后,这是一个用于在构建时验证 .edmx 的 Powershell 脚本示例。
function IsValidViewNode([string]$viewName, [string[]]$keyFields, [int]$typeCheck)
{
[System.Xml.XmlNodeList]$nodelist = $null;
if ( $typeCheck -eq 1 ) {
$nodelist = $Xml.SelectNodes("/edmx:Edmx/edmx:Runtime/edmx:StorageModels/ssdl:Schema/ssdl:EntityType[@Name='$viewName']/ssdl:Key/ssdl:PropertyRef", $nsmgr)
} else
{
$nodelist = $Xml.SelectNodes("/edmx:Edmx/edmx:Runtime/edmx:ConceptualModels/edm:Schema/edm:EntityType[@Name='$viewName']/edm:Key/edm:PropertyRef", $nsmgr)
}
[int] $matchedItems = 0
[int] $unmatchedItems = 0
if ($nodelist -eq $null -or $nodelist.Count -eq 0)
{
return $false;
}
foreach ($node in $nodelist) {
$name = ""
if ($node -ne $null -and $node.Attributes -ne $null -and $node.Attributes -contains "Name" -ne $null )
{
$name = $node.Name
}
#Write-Host $name
if ($keyFields -contains $name) {
$matchedItems++
}
else {
$unmatchedItems++
}
#Write-Host $matchedItems
#Write-Host $unmatchedItems
#Write-Host $keyFields.Length
}
#Right Pad the detail string.,
$resultString = "Primary Keys for $viewName" + (" " * (50 - "Primary Keys for $viewName".Length))
if ( $matchedItems -eq $keyFields.Length -and $unmatchedItems -eq 0 ) {
Write-Host $resultString - Valid
return ""
}
else {
Write-Host $resultString - INVALID
return "$viewName,"
}
}
[string]$PKErrors = ""
# Read the xml file
$xml = [xml](Get-Content 'RALPHDataModel.edmx')
$nsmgr = new-object Xml.XmlNamespaceManager($Xml.NameTable)
$nsmgr.AddNamespace("edmx", "http://schemas.microsoft.com/ado/2009/11/edmx")
$nsmgr.AddNamespace("ssdl", "http://schemas.microsoft.com/ado/2009/11/edm/ssdl")
$nsmgr.AddNamespace("edm", "http://schemas.microsoft.com/ado/2009/11/edm")
<#
***
*** VERIFY PRIMARY KEY COLUMNS FOR VIEWS ***
*** This ensures the developer has run the DataModel.xslt to fix up the .edmx file.
***
#>
$PKErrors = $PKErrors + (IsValidViewNode "Transaction_Detail" ("Asset","Date","Portfolio","System_Reference") 1)
$ExitCode = 0
if ($PKErrors -ne "" ) {
Write-Host "Invalid Primary Keys for Views: " + $PKErrors.TrimEnd(",")
$ExitCode = 100
}
Exit $ExitCode