Database Reference
In-Depth Information
How It Works
In Listing 7-4, we implemented the GetEntities<T>() extension method to retrieve all of the entities in the object
context that are in the Added, Modified, or Unchanged state. Because this may be a common activity in your
application, it makes sense to implement this just once in an extension method. In the implementation of the
GetEntities<T>() method, we use LINQ-to-Entities to filter the set of entries returned by the Entries<T>() method.
The method returns all entries that are not in the Detached state. From these, we filter out relationships and null
entries. From the remaining entries, we select only those of the given type.
There are some important scenarios in which you might want to implement a method like GetEntities<T>() .
For example, in the SavingChanges event, you may want to validate entities that are about to be inserted, modified,
or deleted.
7-6. Generating a Model from the Command Line
Problem
You want to generate a model from the command line.
Solution
To generate a model for a given database from the command line, use the edmgen.exe program. To access the Visual
Studio Command Prompt click Visual Studio 2012 Command Prompt under Microsoft Visual Studio 2012 from the
Start menu.
The Microsoft documentation for the edmgen command provides a complete list of the command line options.
The edmgen command supports a lot of useful command line options The following command, for example,
will generate a model from all of the tables in the given Test database:
edmgen /mode:FullGeneration /project:Test /provider:"System.Data.SqlClient"
/c:"server=localhost;integrated security=true;database=Test;"
Other /mode options are available. One that can be particularly useful in a continuous integration build process is
/mode:ValidateArtifacts . With this option, one or more of the generated layers are validated. You need to use one
or both of the /inssdl or /incsdl options. If you are validating the mapping layer, all three layers must be specified.
You can use one of the /out options to specify the name of the generated file for specific model layers.
For example, using /outcsdl:MyProject.csdl will create the conceptual layer definitions in a file named
MyProject.csdl . There are similar options for the other layers.
How It Works
The edmgen command provides a convenient way to automate some of the build processes, and it is a useful tool for
pregenerating query views and generating separate files for the model layers. One restriction of edmgen is that it does
not provide a way to generate a model based on a subset of the tables in a database.
Using the edmgen command to pregenerate views can be tremendously helpful for application performance.
Before a query can be executed, Entity Framework must build a set of views that it uses to access and query the
database. Without using the edmgen utility, view generation takes place on the first Entity Framework call. If the data
model is relatively small this initialization at first call may pose minimal risk; however, if the data model is large or
particularly complex, then such a performance hit might not be acceptable. In a case such as the latter it may make
sense to pregenerate query views using the edmgen command-line utility.
 
Search WWH ::




Custom Search