Bip
Bip

Reputation: 913

EF Code first existing SQL Azure Database

I successfully created database on my local server using Code First, migrated it to Azure and now I want to connect to that database with using Azure connection string and my existing POCO clases.

My connection string:

<add name="CijenolomciContext" connectionString="Data Source=*.database.windows.net;Initial Catalog=NewCijenolomci;Persist Security Info=True;User ID=Bip*;Password=**;MultipleActiveResultSets=False&quot;" providerName="System.Data.EntityClient" />

but this section of code happens to do nothing on the server:

Item i = new Item();
            i.Name = "Cranberry";
            i.OldPrice = new decimal(25.99);
            i.NewPrice = new decimal(14.99);
            i.SaleStarts = DateTime.Parse("2012-01-21");
            i.CategoryID = 1;
            context.Items.Add(i);
            context.SaveChanges();

How to succesfully add it?

EDIT: This approach happens to modify my local DB, not the Server one.

Upvotes: 1

Views: 742

Answers (2)

Bip
Bip

Reputation: 913

OK. Here it is. In DbContext i inherit base constructor:

 public CijenolomciContext()
        : base(*database name*)
    {
        //additional logic
    }

and despite that i add my connection string in App.config he was always connecting to the local.

I hardcoded Connection string and just add it in the constructor. Now it works! :)

Upvotes: 0

It doesn't look like you're connecting to an Azure database to me. I'd expect a connection string more like this.

Server=tcp:[serverName].database.windows.net;Database=myDataBase;User ID=[LoginForDb]@[serverName];Password=myPassword;Trusted_Connection=False;Encrypt=True;

Use 'username@servername' for the User ID parameter.

See Connection Strings for SQL Azure.

Upvotes: 1

Related Questions