我可以以编程方式删除EDMX模型中的所有实体吗?

时间:2011-08-26 19:08:38

标签: c# entity-framework

我有一种删除所有表格的方法。

我想知道我是否可以:

(1)迭代模型中的元数据来完成同样的事情?

(2)沿途重置身份插入计数?

注意:我不想删除整个数据库,只删除所有表。

这是我想要转换为通用循环的代码:

    public void Delete() {
        using (var db = this.DirectAgentsEntities)
        {
            db.StartingBalances.DeleteObjects(db.StartingBalances);
            ...
            db.RecordSourceTypes.DeleteObjects(db.RecordSourceTypes);
            db.SaveChanges();
        }
    }
static class Extensions {
    static public void DeleteObjects<TEntity>(this ObjectSet<TEntity> set, IEnumerable<TEntity> data) where TEntity : class {
        foreach (var entity in data)
            set.DeleteObject(entity);
    }
}

3 个答案:

答案 0 :(得分:1)

可能更简单:

  • 删除数据库
  • 重新创建数据库
  • 创建表格

edmx文件只是xml,所以你可以遍历它,然后找到你找到的每个表:

  • 从Tablename中删除
  • DBCC CHECKIDENT(表名,重新编号,0)

答案 1 :(得分:1)

只需编写存储过程并将其导入您的edmx。这将是实体框架的最快方式。

答案 2 :(得分:0)

我接受了Shiraz的建议,并根据导入的SPRO和T4模板提出了我的问题的完整答案:

SPROC:

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#>");
        }
<#+
        }
    }
#>