如果您的数据库是SQL Server或SQL Server CE 4.0,则可以使用“Entity Framework Reverse POCO Generator”,该工具可在visualstudiogallery.msdn.microsoft.com上获取。
它可以通过使用TableFilterExclude
、TableFilterInclude
来进行表过滤。
过滤器的工作方式如下:
例如:
TableFilterExclude = new Regex("billing|report");
TableFilterInclude = new Regex("company");
给定以下表格:
任何名称中包含billing或report的表都将被立即排除。 任何名称中包含company的表都将被包括在内。
最终筛选结果为:
我找到了一个解决方案,它可以工作,但需要几个“额外”的步骤:
在项目中,右键单击并选择添加
> 新项
。在左侧选择数据
,然后从项目列表中选择ADO.NET实体数据模型
。
现在,您可以从显示的对话框/向导中选择要从中生成模型的数据库,在这种情况下,您只能选择需要的表、视图和存储过程和函数。
添加模型后,您可以在解决方案资源管理器中展开它,然后应该可以在YourModelName.edmx
--> YourModelName.tt
下找到所需的类。
注意:您将无法拖放源文件,但可以右键单击并选择复制类
。然后可以将它们粘贴到需要它们的位置,并在其名称末尾添加"1"
。因此,您仍需要进行一些轻微的重构(更改文件名、类名和命名空间),但这仍然相当简单。
现在,您可以删除原始模型(即edmx文件)及其下的所有内容。
这不是一个完美的解决方案,但比生成大量表表示并等待VS完成工作要容易得多,特别是如果您有很多不必要的表、视图等。
这是一个老问题,可能已经有了答案,但在谷歌搜索中只得到了这个,所以我想分享我的解决方案——如何使用Entity Framework Power Tools过滤一些表。
After installing VSIX EF Power tools go ahead and right click a solution and select Entity Framework --> Custom Reverse Engineer Templates. A few .tt files will be generated under Sln-root/CodeTemplates/ReverseEngineerCodeFirst, open each one of them.
Context.tt - find the foreach loop at line 27 and paste code for filtering inside of the foreach loop:
// START Filter for specific tables only
string[] filterTables = new string[] { "Motorcycle", "Person" };
if (!filterTables.Contains(set.ElementType.Name.ToString()))
{
continue;
}
// END Filter for specific tables only
这将避免为过滤表数组中未指定的表生成public DbSet<
行。
在foreach循环内的第38行中,粘贴与上述相同的代码。这将避免生成modelBuilder.Configurations.Add
。
Entity.tt - find line 6 and paste code which will return empty and escape generating file for tables not in filterTables array:
// START Filter for specific tables only
string[] filterTables = new string[] { "Motorcycle", "Person" };
if (!filterTables.Contains(efHost.EntityType.Name.ToString()))
{
return "";
}
// END Filter for specific tables only
Mapping.tt - find line 14 and paste the same code as above:
// START Filter for specific tables only
string[] filterTables = new string[] { "Motorcycle", "Person" };
if (!filterTables.Contains(efHost.EntityType.Name.ToString()))
{
return "";
}
// END Filter for specific tables only
Save all .tt files and regenerate the model with "Reverse Engineer Code First".