我可以指定文件名中的实体框架5的LocalDB数据库? [英] Can I specify the filename for a localdb database in entity framework 5?

查看:207
本文介绍了我可以指定文件名中的实体框架5的LocalDB数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我使用实体框架5具有的LocalDB,是否有指定在App.config / Web.config文件的数据库文件名的方法是什么?

If I'm using Entity Framework 5 with LocalDb, is there a way of specifying the filename of the database in the app.config/web.config file?

推荐答案

在进一步调查它看起来非常简单,但阅读文档时,并不清楚。

On further investigation it looks like it is really simple, but isn't clear when reading the docs.

首先你需要有配置的实体框架的一部分

First of all you need to have the entity framework part of the configuration

  <entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
  <parameters>
    <parameter value="v11.0" />
  </parameters>
</defaultConnectionFactory>

一旦你有,那么你需要指定连接字符串。默认情况下,连接字符串名称是上下文的全名。所以在我的测试程序,上下文被称为'DataModel.Context',所以我需要一个连接字符串'DataModel.Context

Once you have that, you then need to specify your connection string. By default the connection string name is the fully qualified name of your context. So in my test app, the context was called 'DataModel.Context', so I need a connection string for 'DataModel.Context'

  <connectionStrings>
<add name="DataModel.Context" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=database;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\database.mdf" providerName="System.Data.SqlClient" />

这则在该项目的数据目录使用文件database.mdf。

This then uses the file 'database.mdf' in the data directory of the project.

这篇关于我可以指定文件名中的实体框架5的LocalDB数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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