我建立了一个在本地运行良好的网站。然后我尝试部署到 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="data source=.\sqlexpress;initial catalog=MyDatabase;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" 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="Data Source=serverstuff.from.azure;Initial Catalog=MyDatabase;User Id=MyUser;Password=MyPassword;App=EntityFramework"" 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(T o) 只是
db.SomeObjects.Add(o);
db.SaveChanges();
我检查了 Azure,数据库已创建,并且有一个表“SomeObject”。
我不明白它试图执行的原因OnModelCreating
。在本地它工作得很好。
这可能是什么原因造成的?