This tutorial shows how to use the ADO.NET Entity Data Model project item to generate an Entity Framework data model. You’ll see how to step through the model wizard to configure the connection, chose the database tables and specify other data model settings.
- [Voiceover] In this demonstration, I'll use the database first approach to create my entity framework data model. I'll be using this project, which is a WPF project, and also, I want to point out that in the server explorer, I've got three connections available over here. I'll be using this one here, this Northwindv13 connection, to connect to the database, and then I'll pull the schema from the database, and use that to generate my classes for the data model. You start by right-clicking on the project and choosing add new item.
Click on the data section, and choose this top item here. ADO.NET entity data model. I'll name it NorthwindModel. Now I have to choose what model, or how to generate the model. I've got four choices here. I can use an entity framework designer. It's created from the database. I can have an empty entity framework designer. That means I'll have to map all the relationships between the data tables and my classes. I can have an empty code-first model, or I can have a code first from database.
My choice is to use this first item. Creates a model based on an existing database. In this screen is choose your data connection, and this is gonna choose one of those three items I showed you over here. Choose this top one. Down at the bottom of the screen, you'll see there's this save connection settings in app-config. So that's this XML file, app.config. Any changes that I save to my connection string, this information right here, will be put inside the app.config file. And I get to give it a name.
I'll just use the default. This dialog is telling me that I'm working with a local DB instance, and I should copy it into my project. Here's the message. "The connection you selected uses a local data file "that is not in the current project. "Would you like to copy the file to your project, "and modify the connection?" So rather than using the connection that points to this, wherever it is on my hard drive, it'll point to it inside my project. I think that sounds good, especially useful for demonstration purposes, so I'll click on yes.
Next choice is "Which version "of entity framework do you use?" I'll use most recent. And when we get to this screen, I get to choose which items inside the database are gonna be used to generate my .NET classes. Let's say I choose this Shippers table only. That'll mean there'll be a single item in my data model. And what'll happen is if I have this pluralize, or singularize generated object names set, see in this tool tip, it says "Makes names in the model more descriptive "and accurate by ensuring that the entity types "are singular, and entity set names are plural." So in other words, there'll be a collection class called Shippers, and there'll be another class created called Shipper, that contains the fields inside of my database, except those'll be mapped to properties in my .NET object.
In my case, I want to use all these items, so I'll go up to the DBO section, and click here. And that'll select all the tables. Then I'll leave everything else at the defaults, and click on finish. This is a security warning that I get, and it's telling me something about text templates, so let's talk about what's happening here. The tooling that Microsoft's created is using something called the t4 templates, and they contain code, and Microsoft's telling me that I'm getting ready to run the code that's inside that text template, and that could be a security risk, especially if I download somebody else's t4 template.
It's asking me if I'm okay with that. "Running this text template "can potentially harm your computer. "Do not run it if you obtained it from an untrusted source." Well, I think this is trusted, so I'll go ahead and say okay, and it's running another t4 template, so I think this one's okay too, so I'll click on the okay button. And finally, we get to the point where I have my entity framework data model. So there's a number of items here. In the next video, I'll detail more what we're seeing here, but let's do a quick overview. Several things happened. It opened up this northwindModel EDMX file, and you can see some sort of diagram here in the center of the screen.
You can see that it added a copy of the database to my project. It also added this EDMX file, entity data model, that's what that stands for, the extension on that. And then, because it added this database, remember that the server explorer works with project-level databases, and will automatically connect to them. That means because I have this Northwindv13 MDF file in my project, it shows an extra connection over here. So this is my connection I deliberately added to server explorer. This is the automatically-added one.
Next up, I'll talk more about what we're seeing here in this Northwind data model.
- Exploring integration with Azure data sources and other Microsoft databases
- Working with SQL Server Express LocalDB
- Connecting to databases with Server Explorer
- Working with database tables
- Manipulating tables with the SQL Server Object Explorer
- Creating queries and custom views
- Creating stored procedures
- Comparing databases
- Building a data project in Visual Studio
- Refactoring a database object
- Deploying the database
- Connecting to Azure databases
- Deploying data projects to Azure
- Integrating Visual Studio with Entity Framework
Skill Level Beginner
Q: This course was updated on 08/23/2017. What changed?
A: The following topics were updated: installing the SSDT features and why LocalDB is essential for developers.