left-icon

Entity Framework Code First Succinctly®
by Ricardo Peres

Previous
Chapter

of
A
A
A

CHAPTER 1

Setting Up

Setting Up


Before We Start

Before you start using EFCF, you need to have its assemblies deployed locally. The distribution model followed by Microsoft and a number of other companies does not depend on old school Windows installers, but instead relies on new technologies such as NuGet and Git. We’ll try to make sense of each of these options in a moment, but before we get to that, make sure you have Visual Studio 2012 installed (any edition including Visual Web Developer Express will work), as well as SQL Server 2008 (any edition including Express) or higher. On SQL Server, create a new database called Succinctly.

Getting Entity Framework Code First from NuGet

NuGet is to .NET package management what Entity Framework is to data access. In a nutshell, it allows Visual Studio projects to have dependencies on software packages—­assemblies, source code files, PowerShell scripts, etc.—stored in remote repositories. EFCF comes in its own assembly, which is deployed out-of-band between regular .NET releases. In order to install it to an existing project, first run the Package Manager Console from the Tools > Library Package Manager and enter the following command.

This is by far the preferred option for deploying Entity Framework Code First.

Tip: This will only work with an existing project, not on an empty solution.

Getting Entity Framework Code First from CodePlex

The second option, for advanced users, is to clone the Entity Framework Code First repository on CodePlex, build the binaries yourself, and manually add a reference to the generated assembly.

First things first, let’s start by cloning the Git repository using your preferred Git client.

git clone https://git01.codeplex.com/entityframework.git

Next, build everything from the command line using the following two commands.

build /t:RestorePackages /t:EnableSkipStrongNames

build

You can also fire up Visual Studio 2012 and open the EntityFramework.sln solution file. This way, you can do your own experimentations with the source code, compile the debug version of the assembly, run the unit tests, etc.

Configuring the Database

Entity Framework is database-agnostic, but the standard version only includes providers for Microsoft technologies. This means only SQL Server 2005+, SQL Server Compact Edition, and SQL Server Express LocalDB are supported. The examples in this book will work on any of these editions. Make sure you have one of them installed and you have the appropriate administrative permissions.

Entity Framework decides on what connection to use by executing the following algorithm.

  • If a connection string is passed in the DbContext’s constructor, then it will try to use that connection string with the default connection factory.
  • If the parameter-less constructor is used, it will look for a connection string in the configuration file, where its name is the same as the context’s class.
  • If no connection string is passed and no connection string with an appropriate name is found in the connection string, it will try to connect to a SQL Server instance named SQLEXPRESS, and a database with the same name as the context class, including namespace.

A connection factory is an implementation of IDbConnectionFactory that sits in a well-known location: Database.DefaultConnectionFactory. This instance can be explicitly set, and should be if a specific database engine requires it. This can be done either by code or by setting a value in the configuration file.

<entityFramework>

  <defaultConnectionFactory

  type="System.Data.Entity.Infrastructure.SqlCeConnectionFactory, EntityFramework" />

</entityFramework>

SQL Server

For connecting to the SQL Server, no special action is required. The default Database.DefaultConnectionFactory is already an instance of SqlConnectionFactory.

If you want to have a connection string in the configuration file, you should use the provider name “System.Data.SqlClient” as per the following example.

<connectionStrings>

  <add name="Succinctly"

  connectionString="Data Source=.\SQLEXPRESS;Integrated Security=SSPI;

  Initial Catalog=Succinctly;MultipleActiveResultSets=true" 

  providerName="System.Data.SqlClient" />

</connectionStrings>

SQL Server Compact Edition

SQL Server Compact Edition (SQLCE) is a small footprint, free and embedded database, which supports practically the same SQL syntax as its full featured sibling. If you want to use it, make sure you have the SQL Server Compact Edition installed; the download is available at http://www.microsoft.com/en-us/sqlserver/editions/2012-editions/compact.aspx.

Tip: SQLCE will only accept a single connection at a time.

If you want to connect to SQLCE, you need to register a connection string using the System.Data.SqlServerCe.4.0 provider.

<connectionStrings>

  <add name="Succinctly"

  connectionString="Data Source=Succinctly.sdf"

  providerName="System.Data.SqlServerCe.4.0" />

</connectionStrings>

If you want to pass the full connection string as parameter to the context, make sure you set the default connection factory to a SqlCeConnectionFactory instance, by using the following code.

Database.DefaultConnectionFactory = new SqlCeConnectionFactory

("System.Data.SqlServerCe.4.0");

Or by the following configuration.

<entityFramework>

  <defaultConnectionFactory

  type="System.Data.Entity.Infrastructure.SqlCeConnectionFactory, EntityFramework">

    <parameters>

      <parameter value="System.Data.SqlServerCe.4.0" />

    </parameters>

  </defaultConnectionFactory>

</entityFramework>

SQLCE will look for and create a file named <database>.sdf in the Bin directory of your project.

SQL Server 2012 Express LocalDB

The LocalDB database released with SQL Server 2012 Express and it is also a small footprint, fully featured server that doesn’t use any services. If you don’t have it already, you can download the installer from http://www.microsoft.com/en-us/download/details.aspx?id=29062.

Tip: LocalDB will only accept a single connection at a time.

For connecting to a LocalDB instance, you will need a connection string very similar to one you would use to connect to SQL Server, including the provider name. Instead of a SQL Server instance, you specify the version of LocalDB to use.

<connectionStrings>

  <add name="Succinctly"

  connectionString="Data Source=(localdb)\v11.0;Integrated Security=SSPI;

  Initial Catalog=Succinctly;MultipleActiveResultSets=true" 

  providerName="System.Data.SqlClient" />

</connectionStrings>

There is no need to configure a default connection factory since LocalDB uses the same as SQL Server, which is the default.

LocalDB will look for and use a database file named <database>.mdf and a transaction log <database>_log.ldf, both located in folder %USERPROFILE%, unless explicitly located somewhere else, by specifying an AttachDBFilename parameter.

<connectionStrings>

  <add name="Succinctly"

  connectionString="Data Source=(localdb)\v11.0;Integrated Security=SSPI;

  MultipleActiveResultSets=true;AttachDBFilename=C:\Windows\Temp\Succinctly.mdf" 

  providerName="System.Data.SqlClient" />

</connectionStrings>

Note: LocalDB files are fully compatible with SQL Server ones.

Scroll To Top
Disclaimer
DISCLAIMER: Web reader is currently in beta. Please report any issues through our support system. PDF and Kindle format files are also available for download.

Previous

Next



You are one step away from downloading ebooks from the Succinctly® series premier collection!
A confirmation has been sent to your email address. Please check and confirm your email subscription to complete the download.