我知道这篇文章有点过时,但是我最近发现了一个解决方案,可以在保存Edmx文件时进行转换。请注意,我们使用的是Visual Studio 2012,Entity Framework 6.0和.Net 4.5,而不是使用Code First。
我们遇到的问题是,通过Entity Framework生成的Views具有额外的主键列,而我们不需要这些列(我们需要的列却没有)。由于视图引用了非确定性函数,因此我们无法在视图上创建唯一性约束。因此,唯一的方法是更新edmx文件以正确获取View的关键列。
为了实现这一点,我更新了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 {
DtdProcessing = DtdProcessing.Prohibit
};
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);
using (MemoryStream ms = new MemoryStream()) {
xsltTransform.Transform(rawDocReader, null, ms);
ms.Position = 0;
xDoc.Load(ms);
}
}
}
}
xDoc.Save(Host.ResolvePath("MyDataModel.edmx"));
#>
以下是我们使用的XSLT文件示例。它有两个作用:1. 将ConcurrencyFixed添加到Transaction_Detail_Base的Version字段中;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"/>
<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>
<xsl:template match="edmx:ConceptualModels/edm:Schema/edm:EntityType[@Name='Transaction_Detail_Base']/edm:Property[@Name='Version']">
<xsl:call-template name="AddConcurrencyAttribute"/>
</xsl:template>
<xsl:template name="AddConcurrencyAttribute">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
<xsl:attribute name="ConcurrencyMode">Fixed</xsl:attribute>
</xsl:copy>
</xsl:template>
<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
}
if ($keyFields -contains $name) {
$matchedItems++
}
else {
$unmatchedItems++
}
}
$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 = ""
$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")
$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