可以';t在其他计算机上打开实体框架生成的数据库文件

本文关键字:框架 实体 文件 数据库 计算机 其他 可以 | 更新日期: 2023-09-27 18:06:41

我首先在应用程序中使用实体框架代码。如果我在第一台机器上创建数据库,并请求用数据库(.mdf和.ldf文件(对我的项目进行git,并尝试在其他机器上克隆此项目,但当我运行此项目时,它无法打开数据库(权限错误(。

是否可以与某些用户在实体脆弱性数据库中创建数据库,或者在没有授权许可的情况下创建数据库?

你能举一些连接字符串或DbContextConfiguration的例子吗?

这是我的例外:

无法创建文件'D:''nullam''nullam。WebUI''App_Data''Nullam。DataLayer。NullamDbContext.mdf'因为它已经存在。更改文件路径或文件名,然后请重试该操作。CREATE DATABASE失败。列出了一些文件名无法创建。检查相关错误。

全球.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; }
    }
}

配置.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>

可以';t在其他计算机上打开实体框架生成的数据库文件

为什么要在启动应用程序时创建数据库时检查数据库。

从GIT和已下载数据库的机器中删除数据库并重新运行应用程序,它将在web.config中指定的位置创建数据库。