Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework Code First Azure connection

I am using Entity Framework Code First 4.3 + Azure and having difficulties connecting to the database. The error I get is the following (on the first query):

Keyword not supported: 'server'.

I have the following connection set up in my Web.config

<configSections>
    type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.3.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
  </configSections>

  <connectionStrings>
    <add name="TestDBContext"
         connectionString="Server=tcp:[SUBSCR].database.windows.net,1433;Database=[MyDB];User ID=[user];Password=[pass];Trusted_Connection=False;Encrypt=True;PersistSecurityInfo=True"
         providerName="System.Data.EntityClient" />
  </connectionStrings>

My DbContext implementing class uses the connection string's name:

public class MyContext : DbContext, IMyContext
    {
        public MyContext()
            : base("TestDBContext")
        {
            Configuration.LazyLoadingEnabled = true;
            Configuration.ProxyCreationEnabled = true;
        }

Can you tell what is going on?

like image 998
Stefan Szasz Avatar asked Feb 11 '12 13:02

Stefan Szasz


People also ask

How do I code first in Entity Framework?

Step 1 − First, create the console application from File → New → Project… Step 2 − Select Windows from the left pane and Console Application from the template pane. Step 3 − Enter EFCodeFirstDemo as the name and select OK. Step 4 − Right-click on your project in the solution explorer and select Manage NuGet Packages…

How do I deploy code first database to Azure?

Enable Code First Migrations in AzureClick More actions > Edit to open the publish settings. In the MyDatabaseContext dropdown, select the database connection for your Azure SQL Database. Select Execute Code First Migrations (runs on application start), then click Save.

How do I code my first migration to an existing database?

Run the Add-Migration InitialCreate command in Package Manager Console. This creates a migration to create the existing schema. Comment out all code in the Up method of the newly created migration. This will allow us to 'apply' the migration to the local database without trying to recreate all the tables etc.


1 Answers

I just had the same problem.

You're missing all the metadata in the connection string that Entity Framework requires. The connection string provided by SQL Azure needs to inserted within the provider connection string parameter of EF's connection string.

<add name="MyConnectionString" connectionString="metadata=res://*/Model.Model.csdl|res://*/Model.Model.ssdl|res://*/Model.Model.msl;provider=System.Data.SqlClient;provider connection string=&quot;[PUT SQL AZURE CONN STRING HERE]&quot;" providerName="System.Data.EntityClient" />

You'll need to use the metadata from your own project. I pulled that metadata from an EF project generating from an existing database.

like image 131
ryanmcdonnell Avatar answered Sep 18 '22 11:09

ryanmcdonnell