我创建了以下控制器和方法来备份我的数据库:
namespace WebUx.Controllers
{
[Authorize(Roles = "Admin")]
//[InitializeSimpleMembership]
public class BackupController : Controller
{
public ActionResult BackupDatabase()
{
var dbPath = Server.MapPath("~/App_Data/DBBackup.bak");
using (var db = new TestDBContext())
{
var cmd = String.Format("BACKUP DATABASE {0} TO DISK='{1}' WITH FORMAT, MEDIANAME='DbBackups', MEDIADESCRIPTION='Media set for {0} database';"
, "TesteDB", dbPath);
db.Database.ExecuteSqlCommand(cmd);
}
return new FilePathResult(dbPath, "application/octet-stream");
}
}
}
但是当我尝试运行时,我收到以下消息:
You must call the "WebSecurity.InitializeDatabaseConnection" method before you call any other method of the "WebSecurity" class. This call should be placed in an _AppStart.cshtml file in the root of your site.
我看到Stackoverflow上的一个解决方案是添加:[InitializeSimpleMembership]我添加了这个但它告诉它无法找到该类并要求我生成一个。
有人可以解释这意味着什么吗?
答案 0 :(得分:0)
还有另一种更容易解决此问题的方法
您可以通过在项目的根目录中创建“_AppStart.cshtml”来实现此目的。
具有以下内容:
@{
if (!WebSecurity.Initialized)
{
WebSecurity.InitializeDatabaseConnection("ConnectionString", "User", "Id",
"UserName", autoCreateTables: true);
}
}
“WebSecurity.InitializeDatabaseConnection”的参数是: