部署EF code首先应用到生产数据库 [英] Deploying EF Code First Apps to Production Database

查看:188
本文介绍了部署EF code首先应用到生产数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用code第一种方法来生成模式写在VS2010简单的.NET MVC 3应用程序。当我将它部署到一个共享的生产服务器我得到了以下错误:[SQLEXCEPTION(0x80131904):创建否认在数据库'师父'DATABASE权限]

I wrote a simple .net mvc 3 app on VS2010 using code first approach to generate the schema. When i deployed it to a shared production server i got the following error: [SqlException (0x80131904): CREATE DATABASE permission denied in database 'master'.]

下面是我的模型code:

Here is my model code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;

namespace Bonappetit.Models
{
public class MenuItem
{
    public int ID { get; set; }
    public string Title { get; set; }
    public string Description { get; set; }
    public decimal Price { get; set; }
}

public class MenuItemDBContext : DbContext
{
    public DbSet<MenuItem> MenuItems { get; set; }
}
}

这里是我的控制器code:

here is my controller code:

 namespace Bonappetit.Controllers
{ 
public class MenuItemsController : Controller
{
    private MenuItemDBContext db = new MenuItemDBContext();

    //
    // GET: /MenuItems/

    public ViewResult Index()
    {
        return View(db.MenuItems.ToList());
    }

    public ActionResult generateJSON()
    {
        return Json(db.MenuItems.ToList(), JsonRequestBehavior.AllowGet);

    }

和这里是我的生产数据库连接字符串

and here is my connection string for the production database

 <connectionStrings>
  <add name="MenuItemDBContext" 
     connectionString="Data Source=localhost\sqlexpress;Initial Catalog=ptafhksz_bonappetit;User ID=ptafhksz_jalal;Password=345d654654Dfdgdfg" 
     providerName="System.Data.SqlClient" xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>

我是新来asp.net mvc的 - 请咨询我怎么跑我对AA的生产应用程序的基础数据库,我不能运行SQL创建或删除

I am new to asp.net mvc - please advice me how run my app against a a production based DB where I cannot run sql create or drop.

谢谢!

推荐答案

在一个共享的环境中,我认为这是正确的,使每个应用程序只能访问自己的数据库,而不是能创建一个数据库。请与共享的环境,你怎么做才能得到一个新的数据库运行起来的供应商。一个容易的选择,以得到它正确初始化是发送DB备份到服务器的管理员,并要求要还原的备份。

In a shared environment I think it is correct to make each application only have access to its own database and not be able to create a database. Check with the supplier of the shared environment how you do to get a new database up and running. An easy option to get it properly initialized is to send a backup of the db to the server's administrator and ask for the backup to be restored.

EF code首先/迁移,可以向不自动创建或在生产环境中,通过运行时自动迁移数据库的<一个href="http://coding.abel.nu/2012/03/$p$pvent-ef-migrations-from-creating-or-changing-the-database/">custom数据库初始化策略。

EF Code First / Migrations can be made to not auto-create or auto-migrate databases when run in a production environment through a custom db initializer strategy.

public class ValidateDatabase<TContext> : IDatabaseInitializer<TContext>
  where TContext : DbContext
{
  public void InitializeDatabase(TContext context)
  {
    if (!context.Database.Exists())
    {
      throw new ConfigurationException(
        "Database does not exist");
    }
    else
    {
      if (!context.Database.CompatibleWithModel(true))
      {
        throw new InvalidOperationException(
          "The database is not compatible with the entity model.");
      }
    }
  }
}

与静态构造函数在你的DbContext启用它:

Enable it with a static constructor in your DbContext:

static MyDbContext()
{
    Database.SetInitializer(new ValidateDatabase<CarsContext>());
}

这篇关于部署EF code首先应用到生产数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆