1

ローカルで完全に機能するウェブサイトを構築しました。次に、Azure にデプロイしようとしました。データベースを使用しないものはすべて正常に動作しますが、データベースにアクセスしようとすると次のようになります。

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)

私のローカル接続文字列は

<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" />

私のリモート接続文字列は

<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)"
/>

DbContextのクラス

//------------------------------------------------------------------------------
// <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(To) はまさに

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

Azureを確認したところ、データベースが作成されており、「SomeObject」というテーブルがあります。

実行しようとしている理由がわかりませんOnModelCreating。ローカルでは問題なく動作します。

何が原因でしょうか?

4

2 に答える 2

2

ConnectionStringAzure パネルに同じ名前の があり、Web.Config.

于 2013-11-11T03:26:01.650 に答える
0

でこの問題が発生しましたWebJobWebApp問題は、接続文字列名が、アプリケーション設定の my の 1 つの接続文字列と同じだったことです。

だから私はこれをWebAppAzure のアプリケーション設定に持っていました:

DatabaseContainer <connectionString>

そして、これは私の中にありました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" />

名前が重複していたため、WebJobmy の接続文字列を使用していたため、接続文字列名を my の別の名前に変更する必要がありました。WebApp

だから私はに変更DatabaseContainerしましたDatabaseContainerJob

于 2016-04-14T08:30:21.950 に答える