如何在解决方案中的所有项目之间共享LocalDB实例? [英] How to share a LocalDB instance across all projects in a solution?

查看:220
本文介绍了如何在解决方案中的所有项目之间共享LocalDB实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的VS 2012解决方案设置:

I have a VS 2012 solution setup like this:

  • EF示范项目
  • EF模型测试项目
  • ASP.NET MVC 4应用程序
  • WCF数据服务项目

在开发过程中,我想使用LocalDB作为EF的后备数据库. MVC和WCF项目都使用EF模型来访问数据库中的数据.我想在所有项目(MVC,WCF和测试)中共享相同的LocalDB实例,但似乎无法正确设置web.config的设置.

During development i want to use LocalDB as the backing database to EF. The MVC & WCF projects both use the EF Model to access the data in the database. I would like to share the same LocalDB instance across all projects (MVC, WCF, & Test) but can't seem to get the web.config's setup right.

我还希望该项目设置可在开发人员的机器上移植,即没有绝对路径.

I'd also like this project setup to be portable across developers machines, IE no absolute paths.

测试项目创建&与c:\ users \\.mdf中的数据库一起使用. MVC和WCF项目希望文件驻留在AppData文件夹中.我一直在根据需要手动进行复制,但是很明显,因为应用程序更改了它们不同步的数据.

The test project creates & works with a database in c:\users\\.mdf. The MVC & WCF projects expect the file to reside in the AppData folder. I've been manually copying it over as needed, but clearly as the apps change data they get out of sync.

关于如何配置项目以相对路径共享同一实例的任何建议或示例?

Any suggestions or examples on how to config the projects to share the same instance with relative paths?

推荐答案

将minhcat_vo的示例放在一起可以使我找出解决方案.

Putting together the example for minhcat_vo made me figure out a solution.

当您未指定与DbContext派生类型名称(或您设置为其注释的任何名称)相匹配的显式连接字符串时,我遇到的问题就发生了.行为在项目类型之间是不同的:

The problem i was encountering happened when you didn't specify an explicit connection string matching the DbContext derived type's name (or whatever you set as it's annotation). The behavior is different between project types:

  • 在一个Test项目中,它使用本地db目录以及DbContext的FQDN,并在\\.mdf处放置一个MDF文件.
  • 在ASP.NET MVC项目中,它尝试从\ app_data \ .mdf加载mdf

在将EF项目移至MySQL时,我偶然发现了该解决方案.在任何情况下,MySQL EF提供程序都不会退回到DefaultConnection连接字符串(我想除非您的DbContext明确选择了它).为了使MySQL-EF正常工作,我必须放入一个与上下文匹配的连接字符串.这使我获得了我一直在寻找的答案.通过为我的所有项目使用以下连接字符串,它们都共享相同的MDF文件/LocalDB实例:

I stumbled onto the solution while moving my EF project to MySQL. The MySQL EF provider will not fall back to the DefaultConnection connection string under any circumstances (i guess unless your DbContext explicitly selected it). To get MySQL-EF working i had to put in a connection string matching the context. This keyed me to the answer i was looking for. By using the following connection string for all my projects they all share the same MDF file/LocalDB instance:

<add name="ThingsContext" providerName="System.Data.SqlClient" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=Things;Integrated Security=SSPI" />

其中ThingsContext是我的项目的DbContext实例.您可以在@ https://github.com/drdamour/SharedLocalDB

Where ThingsContext is the DbContext instance for my project. You can see my example solution @ https://github.com/drdamour/SharedLocalDB

这篇关于如何在解决方案中的所有项目之间共享LocalDB实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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