【问题标题】:Code First Entity Framework and Windows Azure SQLCode First Entity Framework 和 Windows Azure SQL
【发布时间】:2012-07-07 11:26:52
【问题描述】:

我正在尝试将一个非常简单的代码优先示例从我的本地 SQL 移动到 Azure SQL,看起来我已经碰壁了。

这是我正在使用的简单代码:

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="Server=tcp:xxxx.database.windows.net;Database=xxxx;User ID=xxxx@xxxx;Password=xxxx;Trusted_Connection=False;Encrypt=True;" providerName="System.Data.SqlClient" />

现在我有一个简单的控制台应用程序,它执行以下操作:

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

我在 db.SaveChanges(); 行遇到异常说: 无效的对象名称“dbo.Cats”。

堆栈跟踪:

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)

并且没有创建表。 如果有人能指出我正确的方向,我将非常感激,因为我不知道为什么会发生这种情况。

亲切的问候, 奥德里斯。

【问题讨论】:

    标签: entity-framework azure ef-code-first code-first azure-sql-database


    【解决方案1】:

    我今天使用 EF Code First 迁移完成了这项工作。它将最初创建您的表,并在将来启用数据库升级。请参阅此walkthrough 了解更多信息。我认为这是推荐的方法。如果您在包管理器中收到 NullReferenceException,see here

    Another hint(一直向下滚动)是您的连接字符串必须针对 SQL Azure 主数据库,但此信息可能已过时,我没有尝试。

    【讨论】:

      【解决方案2】:

      似乎 Entity Framework 不会在代码优先的方法中为您创建 Azure SQL 表,因此您必须使用模型优先的方法,为数据模型生成 SQL 脚本,然后在基于 Web 的Azure 中可用的 sql 管理器。

      也建议更换

      [id] uniqueidentifier not null,
      

      [id] uniqueidentifier not null default newid(),
      

      对于每个主键,否则数据库生成的键不起作用。

      【讨论】:

        【解决方案3】:

        CodeFirst 与 SQL Azure 配合得很好。

        这曾经是 EF 4.1 的问题 (-) 请确保您使用的是最新版本的 EF(截至今日 NuGet 的 4.3.1)。还要确保您在连接字符串中指定的用户有足够的权限在目标数据库中创建数据库对象。

        【讨论】:

          【解决方案4】:

          在连接字符串中设置PersistSecurityInfo=true 将授予本地计算机在 Azure 中创建数据库的权限。确保在创建数据库后将其删除。

          【讨论】:

            【解决方案5】:

            我遇到了同样的问题,设置 DB Initializer 是最终创建表的解决方案。
            只需将以下内容放入Global.asax.cs:

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

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2023-04-02
              • 1970-01-01
              • 1970-01-01
              • 2011-03-03
              • 2011-12-24
              • 2015-10-22
              相关资源
              最近更新 更多