c# - 如何将 EF Code First 数据库发布到 Azure

标签 c# asp.net entity-framework web-services azure

我在将 Entity Framework 数据库发布到 Azure 时遇到问题。

我正在使用 .NET Framework 4.5 和 EF 6.1.3。我的项目设置方式如下并使用代码优先方法:

  • 领域实体(数据库实体)的类库
  • DTO 的类库
  • 存储库的类库,我在其中使用实体执行所有 CRUD 操作
  • 仅用于 Web 服务的 Web API 项目(没有演示,只有静态 Controller )。

我的 Web 服务项目引用了存储库库和 DTO 库,我从未显式创建数据库,因为 EF 使用 SQL Server Express 为我创建了它,我相信,我按如下方式设置 web.config:

<connectionStrings>
 <add name="Database" connectionString="Server=(localdb)\v11.0;Database=Database;Trusted_Connection=True;" providerName="System.Data.SqlClient" />
</connectionStrings>

在我的项目中,数据库是与所有表和关系一起创建的。我在存储库库中添加了一些初始迁移,如下所示

namespace Project.Repository.Migrations
{
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Migrations;
using System.Linq;

internal sealed class Configuration : DbMigrationsConfiguration<Project.Repository.ProjectContext>
{
    public Configuration()
    {
        AutomaticMigrationsEnabled = true;
        AutomaticMigrationDataLossAllowed = true;
    }

    protected override void Seed(Project.Repository.ProjectContext context)
    {
        Guid userId = new Guid();
        Guid restaurantId = new Guid();
        context.Users.AddOrUpdate(
            u => u.Id, new Domain.Security.User { Id = userId, Age = 18, Description = "I like restaurants", Email = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="215452445361464c40484d0f424e4c" rel="noreferrer noopener nofollow">[email protected]</a>", Password = "12345", Username = "someuser" }
            );

        context.Restaurants.AddOrUpdate(
                r => r.Id,
                new Domain.Restaurants.Restaurant
                {
                    Id = restaurantId,
                    Name = "mcdonalds",
                    Description = "best restaurant",
                    FacebookUrl = "www.facebook.com",
                    GoogleUrl = "www.google.com",
                    InstagramUrl = "www.instagram.com",
                    TwitterUrl = "www.twitter.com",
                    Votes = 3.5f,
                    UsersWhoLike = new List<Domain.Security.User>() { context.Users.FirstOrDefault(u => u.Id == userId) }
                }
            );
        context.SaveChanges();
    }
}
}

当我设置将项目发布到我的 azure 网站时,网络服务发布正常,但数据库不会发布,或者至少每当我使用 website/api/restaurants 调用餐厅服务时,这是获取餐厅列表的途径说。

{"Message":"An error has occurred."}

我的 azure 数据库连接字符串设置如下:

Data Source=tcp:someidentifier.database.windows.net,1433;Initial Catalog=Database;Integrated Security=False;User ID=Database@someidentifier;Password=xxxxxx;Connect Timeout=30;Encrypt=True

我不知道发生了什么,也不知道 azure 是否真的在为数据库做种,我遗漏了什么或者可能是我的错误?

最佳答案

首先,转到您的 asp.net webconfig 并将该值设置为“关闭”:

<system.web><customErrors mode="Off"/></system.web> 

您将看到信息丰富的蓝屏死机,并有机会找出到底出了什么问题。如果 YoD 不清楚,请将其发布在您的答案中。

关于c# - 如何将 EF Code First 数据库发布到 Azure,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33058364/

相关文章:

sql-server - 尝试读取或写入连接到 SQL Server 的 protected 内存

c# - 克隆对象的好方法

c# - 在 WP7 中编译 Google Data API .NET 库

c# - 排除 ef 核心迁移文件的代码覆盖率

c# - 3rd party是302重定向到我的网站,如何停止?

c# - 当值不相同时违反主键

c# - ICommand 缺少 namespace 或类型

asp.net - Microsoft 重写模块 - 在 url 上强制 www 或从 url 中删除 www

c# - 可以设置RequestValidationMode ="2.0"页面级别吗?

c# - 如何选择最后一小时和groupby分钟?