Code First Entity Framework and Windows Azure SQL

azure azure-sql-database code-first ef-code-first entity-framework


I am trying move a very simple code first example from my local SQL on to Azure SQL and as it appears I've hit a brick wall.

This is the simple code that I am using:

public class Cat
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Guid ID { get; set; }
    public string Name { get; set; }

**Context class:**

public class CatDbContext : DbContext
    public PersonDbContext()
        : base("name=MyConnectionString")
    { }
    public DbSet<Cat> Cats { get; set; }

My connection string:
 <add name="MyConnectionString" connectionString=";Database=xxxx;User ID=xxxx@xxxx;Password=xxxx;Trusted_Connection=False;Encrypt=True;" providerName="System.Data.SqlClient" />

Now I have a simple console app which does the following:

        CatDbContext db = new CatDbContext();
        db.Cats.Add(new Cat { Name = "Garfield", ID = Guid.NewGuid() });

And I am getting an exception at line db.SaveChanges(); saying: Invalid object name 'dbo.Cats'.

Stack trace:

at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning()
at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
at System.Data.SqlClient.SqlDataReader.ConsumeMetaData()
at System.Data.SqlClient.SqlDataReader.get_MetaData()
at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)
at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
at System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior)
at System.Data.Mapping.Update.Internal.DynamicUpdateCommand.Execute(UpdateTranslator translator, EntityConnection connection, Dictionary`2 identifierValues, List`1 generatedValues)
at System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter)

And no tables are created. I will be very thankful if someone could point me in the right direction, as I have no clue why this is happening.

Kind regards, Audrius.

7/7/2012 11:26:52 AM

Accepted Answer

I've done this today using EF Code First Migrations. It will create your tables initially and also enable DB upgrades in the future. Refer to this walkthrough for more information. I think this is the recommended approach. Should you get a NullReferenceException in the package manager, see here.

Another hint (scroll all the way down) is that your connection string has to target the SQL Azure master db, but this information may be outdated and I didn't try it.

5/23/2017 12:04:43 PM

Popular Answer

I had the same issue, and setting up a DB Initializer was the solution that finally created the tables.
Just put the following in Global.asax.cs:

Database.SetInitializer<MyContext>(new CreateDatabaseIfNotExists<MyContext>());

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow