Generating Code

Likely the whole reason you’ve created the entities in the designer is that you eventually want to generate code for some application. Nothing could be easier.

If you’ve set the designer to automatically generate code on save, just save the model. Your code will be created.

Otherwise, you can right click anywhere and choose Generate Code, or use the shortcut key (Ctrl-G by default).

If previously generated files no longer exist, the T4 templates attempt to clean them up. They err on the conservative side, though… we wouldn’t want to start deleting code that you spent time creating, would we? So there may be a few strays left behind when you delete entities or enumerations and regenerate.

Code is generated where you told it go (or, if you haven’t, in the same directory and solution folder where the model file lives).

Since code is generated from the T4 files, you can also do the standard Run Custom Tool command on the .tt file and get the same result.

Understanding the code

While you can easily change the code that’s generated, you might want to know exactly what you’ll get if you use the code that’s generated out-of-the-box. If so, read on!

DbContext

Properties & MethodsDescription
[DbContext Name].generated.cs
(various DbSets)One per class in the model
ConnectionStringStatic string that will be used as the default connection string
ConstructorsConstructors are generated for each constructor signature of the DbContext class. They initialize various properties that were entered into the designer and, for those things the designer couldn't anticipate, executes a call to CustomInit(), a partial method you can choose to implement to do anything else you need to do in the constructor.
OnModelCreatingImplSince OnModelCreating is generated, this is a partial method you can optionally implement to do some custom work just before the DbModelBuilder functions are called.
OnModelCreatedImplSame as the above, but called just after the DbModelBuilder functions are executed.
[DbContext Name]DatabaseInitializer.generated.cs
Generated to derive from the initializer class set in the model. Won't be generated if None was chosen. No properties or methods overridden.
[DbContext Name]DbMigrationConfiguration.generated.cs
ConstructorSets up values indicated in the designer and calls the partial method Init() that you can optionally implement to do custom work. Seed methods can be added to a partial of this class in another file.

Entities

Properties & MethodsDescription
[Entity Name].generated.cs
Default ConstructorAlways generated (because Entity Framework needs it), but will be created as protected if there are any required properties or associations in the class. Calls the partial method Init() that you can optionally implement to do custom work.
Other ConstructorGenerated if there are any required properties. Parameters will include those properties, and code in the body will check to ensure they're not null or the default value for their type. Calls the partial method Init() that you can optionally implement to do custom work.
CreateA static convenience method that creates and returns an entity. Useful for LINQ queries where constructor parameters are necessary, since there are requirements to only use parameterless constructors when querying a database.
(listed properties)Declarations of the properties and associations in the model.

Next Step

Customizing Code Generation