WHAT IT IS: Entity Framework’s Code First strategy has some beautiful syntactic sugar, but how it creates and manages Model classes isn’t always intuitive to find.
WHY WE CARE: EF Code First can be a time-saving strategy when your database schema is still in flux at the beginning of a project’s life cycle.
HOW IT DOES WHAT IT DOES: Syntax conventions and EF infrastructure provide automatic database and table configuration features.
GETTING STARTED:
Whatever strategy you use to build model classes, Entity Framework still functions as an ORM tool, mapping the relational tables in your database to object-oriented classes in your application. When you use the Code First strategy, you build these classes using public properties to represent (eventual) table columns.
(All examples in this post are produced using an MVC 5 application in Visual Studio 2017)
This class represents a typical poker player
When we run the application targeting a SQL Server database, the table that will be created is named PokerPlayers, and looks like this:
Notice that the Id property was created as a primary key field… But how?
- EF Code First Mystery #1– Primary Keys: This is all based on a naming convention. If I name a numeric or GUID property as ID or (what would also work in my case) PokerPlayerID, EF automatically creates an auto-incrementing primary key field in the table.
In order to get to this point there’s more code, of course. I also created a class that inherits from DbContext, as the local object that manages the communication to/from my data store, as well as an initializer class that specifies how to create the database in the first place. However, you can do this all– successfully– even if you don’t create a connection string anywhere in the application.
- EF Code First Mystery #2– Database Creation: Depending on what version of EF and Visual Studio you’re using, you’ll get either a new SQLExpress or localdb database (The developer instances that usually install with Visual Studio). To discover the exact location, you can add a Debug statement to the DbContext class, where the Connection object lives.
What you’ll get here is a database named for the fully-qualified name of the DbContext class. For this demo, EF created a database named CodeFirstDemo.Models.PokerContext, where CodeFirstDemo.Models is the namespace name of the PokerContext class.
If you’d rather manage the connection yourself, you have a couple options
- EF Code First Mystery #3 — Managing the Connection String: If you add a connection string to the root web.config file that exactly matches the class name of the DbContext class, EF will automatically use it to create the new database.
You can also call the DbContext class’ base constructor, and pass either the name of an existing connection string from the web.config file…
… or put an ad hoc connection string in yourself.
NEXT LEVEL: As your model classes change throughout the development life cycle, you can take advantage of EF Code-First Migrations to control the way your database updates in response to Model class changes.
No Comments Yet