How to create dynamic database in entity framework with specified path?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-12 03:09:58

问题


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.


回答1:


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;

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);


来源:https://stackoverflow.com/questions/10051843/how-to-create-dynamic-database-in-entity-framework-with-specified-path

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!