snetite .net核心发布到azure失败

时间:2017-04-07 04:02:17

标签: c# sqlite azure asp.net-core

我正在尝试发布使用sqlite到Azure的代码优先的webapp。现在,该应用程序在本地工作正常。但是当我发布时,似乎没有正确创建数据库文件,我收到以下错误:

  

SqliteException:SQLite错误1:'没有这样的表:博客'。   Microsoft.Data.Sqlite.Interop.MarshalEx.ThrowExceptionForRC(int rc,Sqlite3Handle db)

我不确定为什么没有创建Blogs表(我假设其余的都没有创建)。

我的ApplicationDbContext.cs看起来像这样:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
        : base(options)
    {

    }

    public ApplicationDbContext()
    {
        Database.EnsureCreated();
        Database.Migrate();
    }

    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);
    }

    public DbSet<Publication> Publications { get; set; }
    public DbSet<Blog> Blogs { get; set; }
    public DbSet<Student> Students { get; set; }
    public DbSet<ApplicationUser> ApplicationUser { get; set; }
}

配置:

    public void ConfigureServices(IServiceCollection services)
    {
        // Add framework services.
        services.AddDbContext<ApplicationDbContext>(options =>
            options.UseSqlite(Configuration.GetConnectionString("DefaultConnection"))
        );
    }
{p}和DefaultConnection中的appsettings.json

  "ConnectionStrings": {
    "DefaultConnection": "Filename=.\\mydb.sqlite"
  },

此外,当我下载mydb.sqlite并打开它时,它完全为空。

1 个答案:

答案 0 :(得分:2)

@Tseng你对EnsureCreated的直觉是正确的。我最后通过清除构造函数并将其添加到Configure

Startup.cs方法的 end 来解决此问题
    var serviceScopeFactory = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>()
    using (var serviceScope = serviceScopeFactory.CreateScope())
    {
        var dbContext = serviceScope.ServiceProvider.GetService<ApplicationDbContext>();
        dbContext.Database.EnsureCreated();
    }

我找到了答案here