While working with Entity Framework, I consider it a best practice to create a stand-alone dll that contains all the code Entity Framework (EF) generates to communicate with the database. I would use the Entity Framework Wizard in Visual Studio to point EF to a database, let it generate the code, and then compile it into a dll. This dll would then be referenced by my project(s) that would like to use EF to communicate with my database. I wanted a tool to automate this process of generating the EF dll, and this is where EfGen comes in. You give it a connection string, and it outputs a DLL named
efmodel.dllby default. You can then reference this dll in your projects, and create an instance of following class to query the database:
var db = new EFContext(); // inherits DbContext; use the --context option (see below) to customize the class name
Comparison with edmgen.exe:
DbContextwhereas the old method generated a class that inherited
ObjectContextEdmgen.exe uses the old method, while efgen.exe uses the new method
Here is complete usage of the tool:
Usage: EfGen [<options>]
e.g., EfGen -c "data source=machine-name;initial catalog=database-name;integrated security=True;MultipleActiveResultSets=True" --noViews --noFunc
where:
-c, --connectionString Required. Connection String
-o, --out (Default: EfModel.dll) Name of output dll
-n, --namespace (Default: EFModel) Namespace
-p, --provider (Default: System.Data.SqlClient) The provider used
to connect to the data store
--context (Default: EFContext) Name of class that inherits
from DbContext. Also known as the container class
--noTables Exclude database tables from being included in the
entity framework model
--noViews Exclude database views from being included in the
entity framework model
--noFunc Exclude database functions from being included in
the entity framework model
--nofk Do not generate foreign key properties in the
entity framework model
--noXml Suppress generation of XML Documentation file
--stages (Default: 3) An integer between 1 and 3; if stage =
1, then only .edmx file is generated; if stage = 2,
then .edmx file + .cs files are generated; if stage
= 3, then .edmx, .cs files are generated, and .cs
files are compiled into a dll
--incFolder (Default: $(ProgramFiles)\Microsoft Visual Studio
11.0\Common7\IDE\Extensions\Microsoft\Entity
Framework Tools\Templates\Includes) Folder where
EF.Utility.CS.ttinclude is located
--t4vs (Default: $(ProgramFiles)\Microsoft Visual Studio
11.0\Common7\IDE\Microsoft.Data.Entity.Design.dll)
T4VSHost for CleanupBehavior; see
http://bit.ly/YtRl8Y
--efPath (Default:
$(windir)\Microsoft.Net\assembly\GAC_MSIL\EntityFram
ework\v4.0_5.0.0.0__b77a5c561934e089) Path where
EntityFramework.dll is located
--version Assembly version of the generated dll e.g.,
"1.2.3.4"
--help Display this help screen.