将EF Code First Apps部署到生产数据库 [英] Deploying EF Code First Apps to Production Database
问题描述
这是我的型号代码:
using System;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Data.Entity;
命名空间Bonappetit.Models
{
public class MenuItem
{
public int ID {get;组; }
public string标题{get;组; }
public string描述{get;组; }
public decimal价格{get;组; }
}
public class MenuItemDBContext:DbContext
{
public DbSet< MenuItem> MenuItems {get;组;
}
}
这是我的控制器代码:
命名空间Bonappetit.Controllers
{
public class MenuItemsController:Controller
{
private MenuItemDBContext db = new MenuItemDBContext();
//
// GET:/ MenuItems /
public ViewResult Index()
{
return View(db.MenuItems。 ToList());
}
public ActionResult generateJSON()
{
返回Json(db.MenuItems.ToList(),JsonRequestBehavior.AllowGet);
}
这里是生产数据库的连接字符串
< connectionStrings>
< add name =MenuItemDBContext
connectionString =Data Source = localhost\sqlexpress;初始目录= ptafhksz_bonappetit;用户ID = ptafhksz_jalal;密码= 345d654654Dfdgdfg
providerName =System。 Data.SqlClientxdt:Transform =SetAttributesxdt:Locator =Match(name)/>
< / connectionStrings>
我是asp.net mvc的新手 - 请教我如何运行我的应用程序对一个基于生产的数据库我不能运行sql create或drop。
谢谢!
在共享环境中,我认为使每个应用程序只能访问自己的数据库,而无法创建数据库是正确的。与共享环境的供应商一起检查如何使新数据库正常运行。正确初始化的一个简单的选项是将数据库的备份发送到服务器的管理员,并要求备份恢复。
EF代码第一个/迁移可以在通过自定义db初始化策略。
public class ValidateDatabase< ; TContext> :IDatabaseInitializer< TContext>
其中TContext:DbContext
{
public void InitializeDatabase(TContext context)
{
if(!context.Database.Exists())
{
抛出新的ConfigurationException(
数据库不存在);
}
else
{
if(!context.Database.CompatibleWithModel(true))
{
throw new InvalidOperationException(
数据库与实体模型不兼容。);
}
}
}
}
启用它与您的DbContext中的静态构造函数:
static MyDbContext()
{
Database.SetInitializer新的ValidateDatabase< CarsContext>());
}
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'.]
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; }
}
}
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>
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.
Thanks!
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 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.");
}
}
}
}
Enable it with a static constructor in your DbContext:
static MyDbContext()
{
Database.SetInitializer(new ValidateDatabase<CarsContext>());
}
这篇关于将EF Code First Apps部署到生产数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!