![]() Now we’ll write our model classes and use EF “code first” to map them to our Northwind database. SQL files it includes to install it into a SQL database, or copy the Northwind.mdf SQL Express file into the \App_Data directory of your application: If you don’t have Northwind already installed then you can download it here. You can skip this step if you have a SQL Server database with Northwind (or another database) installed. ![]() After adding this reference you’ll see it show up in your project’s references window like below: You’ll reference the “.dll” assembly that is installed within the “\Program Files\Microsoft ADO.NET Entity Framework Feature CTP4\Binaries\” directory when you download and install the EF Code First library. Right click on the “references” node within the Solution Explorer and choose “Add Reference”. Our next step will be to add a reference to the EF Code First library to our project. Step 2: Reference the EF Code First Assembly We don’t need these default files (we could instead just use the “Empty ASP.NET Web Application” project template) – but they’ll make our simple app look a little prettier by default so we’ll use them. When it is created you’ll find it contains a few default files within it: The new “ASP.NET Web Application” project in VS 2010 is a nice starter template that provides a default master-page layout with CSS design (I blogged about this new starter project template in a previous blog post). ![]() We’ll use “File->New Project” within VS 2010 (or the free Visual Web Developer 2010 Express) and choose the “ASP.NET Web application” project template to create the new application. Note that all of the EF concepts are identical regardless of whichever type of ASP.NET application you use. My previous two EF “code first” blog posts used ASP.NET MVC – for this blog post I’ll use ASP.NET Web Forms. Let’s begin by creating a new ASP.NET Web Application Project. Step 1: Create a new ASP.NET Web Application Project In particular, it enables you to use clean “plain old classes” (aka POCO) for your model objects, and cleanly map them to/from the database using either the default mapping conventions or by overriding them with custom schema mapping rules.īelow are step by step instructions on how you can use EF “Code First” with an existing database. In today’s blog post I’m going to cover a question that several people asked me recently, which is: “how do I use EF code-first with an existing database?” Using EF Code-First with an Existing DatabaseĮF “Code First” works great with existing databases, and enables a very nice code-centric development approach with them. I then did a second custom database schema mapping blog post that discussed how you can override the default persistence mapping rules, and enable custom database schemas. These default conventions work very well for new applications, and enable you to avoid having to explicitly configure anything in order to map classes to/from a database. In my initial blog post I introduced EF “code-first” and demonstrated how to use the default EF4 mapping conventions to create a new database. ![]()
0 Comments
Leave a Reply. |