如何在实体框架中创建具有指定路径的动态数据库? [英] How to create dynamic database in entity framework with specified path?

查看:230
本文介绍了如何在实体框架中创建具有指定路径的动态数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我在Entity Framework中使用CreateDatabase()方法创建动态数据库
它执行成功,并在以下路径创建数据库:
c:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA
但我想在我的应用程序的目录中创建数据库。
怎么办?
请帮助我。

currently i create dynamic database using CreateDatabase() method in Entity Framework. it executes sucessfully and it creates database at following path: "c:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA" but i want to create database in my application's directory. how to do this? please help me.

推荐答案

通常,所有数据库都是在SQL Server中配置的默认路径上创建的。只有使用SQL Server Express,并且如果定义了连接字符串,该连接字符串指定要在数据文件夹中创建的数据库,它将使用您的应用程序文件夹。它将仅适用于SQL Server Express。确保连接字符串的内部部分看起来像:

Normally all databases are created on default path configured in SQL Server. Only if you are using SQL Server Express and if you define connection string which specifies database to be created in your data folder it will use your application folder. It will work only with SQL Server Express. Make sure that inner part of connection string looks like:

Server=.\SQLExpress;AttachDbFilename=|DataDirectory|\YourDbFile.mdf;Database=YourDbName; Integrated Security=SSPI;

其中 | DataDirectory | 占位符替换为您的数据目录的路径。对于Web应用程序,它应该是App_Data,对于独立应用程序,它应该是应用程序的文件夹。您还可以使用直接在连接字符串中的任何路径或通过使用以下命令来控制DataDirectory解析:

Where |DataDirectory| placeholder which will be replaced by path to your data directory. For web application it should be App_Data and for standalone applications it should be application's folder. You can also use any path directly in connection string or control DataDirectory resolution from code by using:

AppDomain.CurrentDomain.SetData("DataDirectory", YourPath);

这篇关于如何在实体框架中创建具有指定路径的动态数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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