Question

I built a website that is working perfectly locally. Then I tried to deploy to Azure. Everything that doesn't use a database works fine, but when I try to access the database I get:

System.Data.Entity.Infrastructure.UnintentionalCodeFirstException:
Code generated using the T4 templates for Database First and Model First
development may not work correctly if used in Code First mode...

   at MySite.Infrastructure.DatabaseContainer.OnModelCreating(DbModelBuilder modelBuilder)
   at System.Data.Entity.Internal.LazyInternalContext.CreateModelBuilder()
   at System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext)
   at System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input)
   at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
   at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
   at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
   at System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext()
   at System.Data.Entity.Internal.Linq.InternalSet`1.ActOnSet(Action action, EntityState newState, Object entity, String methodName)
   at System.Data.Entity.Internal.Linq.InternalSet`1.Add(Object entity)
   at System.Data.Entity.DbSet`1.Add(TEntity entity)
   at MySite.Services.ServiceBase`1.Add(T o)
   at MySite.Controllers.SomeController.New(SomeObject objectParam)

My local connection string is

<add name="DatabaseContainer" connectionString="metadata=res://MySite.Data/Database.csdl|res://MySite.Data/Database.ssdl|res://MySite.Data/Database.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=.\sqlexpress;initial catalog=MyDatabase;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />

My remote connection string is

<add name="DatabaseContainer" connectionString="metadata=res://MySite.Data/Database.csdl|res://MySite.Data/Database.ssdl|res://MySite.Data/Database.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=serverstuff.from.azure;Initial Catalog=MyDatabase;User Id=MyUser;Password=MyPassword;App=EntityFramework&quot;" providerName="System.Data.EntityClient"
  xdt:Transform="SetAttributes" xdt:Locator="Match(name)"
/>

My DbContext class

//------------------------------------------------------------------------------
// <auto-generated>
//    This code was generated from a template.
//
//    Manual changes to this file may cause unexpected behavior in your application.
//    Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace MySite.Infrastructure
{

    using MySite.Models;

    using System;
    using System.Data.Entity;
    using System.Data.Entity.Infrastructure;

    public partial class DatabaseContainer : DbContext
    {
        public DatabaseContainer()
            : base("name=DatabaseContainer")
        {
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            throw new UnintentionalCodeFirstException();
        }

        public DbSet<SomeObject> SomeObjects { get; set; }
    }
}

MySite.Services.ServiceBase`1.Add(T o) is just

db.SomeObjects.Add(o);
db.SaveChanges();

I checked Azure, the database is created and there is a table "SomeObject".

I do not understand the reason why it is trying to execute OnModelCreating. Locally it works just fine.

What could be causing this?

Was it helpful?

Solution

Just found out that I had a ConnectionString with the same name on Azure panel, which was overriding my configuration in Web.Config.

OTHER TIPS

I had this issue on my WebJob. The issue was that the connection string name was same as one connection string for my WebApp in Application Settings.

So I had this in Application Settings for WebApp in Azure:

DatabaseContainer <connectionString>

And this was in my WebJob:

<add name="DatabaseContainer" connectionString="metadata=res://*/TMSModel.csdl|res://*/TMSModel.ssdl|res://*/TMSModel.msl;provider=System.Data.SqlClient;provider connection string=myconnectionstring;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />

Had to change the connection string name to something else in my WebJob, because it was using the connection string of my WebApp, because the names overlapped.

So I changed DatabaseContainer to DatabaseContainerJob.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top