我把设拉子的建议,并拥有完整的回答我的问题作为一个进口的存储过程和T4模板想出了:
存储过程:
ALTER procedure dbo.DeleteTable
(
@tableName varchar(500)
)
AS
declare @sql varchar(1000)
set @sql = 'delete from ' + @tableName
exec (@sql)
return
T4模板:
<#@ template debug="true" hostSpecific="true" #>
<#@ output extension=".cs" #>
<#@ Assembly Name="System.Core.dll" #>
<#@ Assembly Name="System.Xml.dll" #>
<#@ Assembly Name="System.Xml.Linq.dll" #>
<#@ Assembly Name="System.Windows.Forms.dll" #>
<#@ import namespace="System" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Diagnostics" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Xml.Linq" #>
<#@ import namespace="System.Collections" #>
<#@ import namespace="System.Collections.Generic" #>
<#
string ns = "EomApp1.Formss.AB2.Model";
string cn = "Util";
string edmxFile = "\\ABModel.edmx";
string inputContent = System.IO.File.ReadAllText(System.IO.Path.GetDirectoryName(this.Host.TemplateFile) + edmxFile);
#>
namespace <#=ns#>
{
static public class <#=cn#>
{
<#DeleteTablesCodeGen(inputContent);#>
}
}
<#+
void DeleteTablesCodeGen(string input)
{
XNamespace edmxNS = "http://schemas.microsoft.com/ado/2008/10/edmx";
XNamespace ssdlNS ="http://schemas.microsoft.com/ado/2009/02/edm/ssdl";
System.Xml.Linq.XDocument xd = System.Xml.Linq.XDocument.Parse(input);
var tables =
from c in xd.Root
.Element(edmxNS + "Runtime")
.Element(edmxNS + "StorageModels")
.Element(ssdlNS + "Schema")
.Element(ssdlNS + "EntityContainer")
.Elements(ssdlNS + "EntitySet")
select new
{
Name = c.Attribute("Name").Value,
EntitySet = c
};
foreach (var table in tables)
{
#>
static public void Delete<#=table.Name#>(DirectAgentsEntities model)
{
model.DeleteTable("<#=table.Name#>");
}
<#+
}
}
#>
感谢您的建议,它让我回到了我的答案 –