[英]Can't open database file generated by Entity Framework on other machine
我首先在應用程序中使用實體框架代碼。 如果我在第一台機器上創建數據庫,然后用數據庫(.mdf和.ldf文件)攪動我的項目,然后嘗試在其他機器上克隆該項目,但是當我運行該項目時,它無法打開數據庫(權限錯誤) 。
它是否可能與某些用戶一起在實體框架數據庫中創建,還是在沒有自動許可權限的情況下創建數據庫?
可以給你一些連接字符串或DbContextConfiguration的例子嗎?
這是我的例外:
無法創建文件'D:\\ nullam \\ Nullam.WebUI \\ App_Data \\ Nullam.DataLayer.NullamDbContext.mdf',因為該文件已存在。 更改文件路徑或文件名,然后重試該操作。 CREATE DATABASE失敗。 列出的某些文件名無法創建。 檢查相關錯誤。
Global.asax
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Nullam.DataLayer;
namespace Nullam.WebUI
{
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RouteConfig.RegisterRoutes(RouteTable.Routes);
Database.SetInitializer(new DropCreateDatabaseIfModelChanges<NullamDbContext>());
}
}
}
NullamDbContext.cs
using Nullam.Domain.Entities;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Nullam.DataLayer
{
public class NullamDbContext : DbContext
{
public NullamDbContext() : base("NullamDb")
{
}
public DbSet<Member> Members { get; set; }
public DbSet<SocialEvent> SocialEvents { get; set; }
}
}
Configuration.cs
using System;
using System.Collections.Generic;
using System.Data.Entity.Migrations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Nullam.DataLayer.Migrations
{
class Configuration : DbMigrationsConfiguration<NullamDbContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = true;
AutomaticMigrationDataLossAllowed = true;
}
protected override void Seed(NullamDbContext context)
{
// This method will be called after migrating to the latest version.
// You can use the DbSet<T>.AddOrUpdate() helper extension method
// to avoid creating duplicate seed data. E.g.
//
// context.People.AddOrUpdate(
// p => p.FullName,
// new Person { FullName = "Andrew Peters" },
// new Person { FullName = "Brice Lambson" },
// new Person { FullName = "Rowan Miller" }
// );
//
}
}
}
Web.config
<connectionStrings>
<add name="NullamDb"
providerName="System.Data.SqlClient"
connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|Nullam.DataLayer.NullamDbContext.mdf;Integrated Security=SSPI;"/>
</connectionStrings>
您為什么要在啟動應用程序時檢入將在何時創建數據庫。
從GIT中刪除數據庫,然后將其下載到該計算機上,然后重新運行該應用程序,它將在web.config中指定的位置創建數據庫。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.