在.mdf文件实体框架 [英] Entity Framework on .mdf file

查看:140
本文介绍了在.mdf文件实体框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作目前一些项目,我必须使用本地数据库。所以,我创建了一个新的基于服务的数据库(没有表ATM)。然后,我想补充实体框架的支持。

I am working on some project at the moment and I have to use local database. So, I created a new service-based database (no tables atm). Then I wanted to add Entity Framework support.

由于我从来没有使用过实体框架,我指的是该链接: http://msdn.microsoft.com/en-us/data/jj200620.aspx

Because I never used Entity Framework before, I was referring to that link: http://msdn.microsoft.com/en-us/data/jj200620.aspx.

一切正常,但在这里它变得复杂。我创建了的DataContext DbSet 类里面。但是,当我跑我的单元测试中,对创建的LocalDB表(里面没有我的密度纤维板文件)。

Everything is OK, but here it gets complicated. I created my DataContext class with DbSet inside it. But, when I run my unit test, table is created on Localdb (not inside my .mdf file).

该怎么办?

我敢肯定,我没有选择正确使用哪个数据库(实际上这样做已经3次),但仍都在创建的LocalDB数据表。什么我做错了吗?

I am pretty sure, that I did choose which database to use correctly (actually did that 3 times already), but still, data tables are created on LocalDb. What I am doing wrong here?

我与初学者(仅被使用学说ORM)。否则,我可以插入数据和所有,它只是在错误的数据库。

I am complete beginner with that (been only using doctrine ORM). Otherwise I can insert data and all, it is just on the wrong database.

推荐答案

当在EF你做的代码先开发,你可以强制EF到永远只考虑一个连接字符串名称。

When your doing code first development in EF, you can force EF to only ever consider one connection string name.

一对EF数据上下文父类的构造函数(其中有相当多的过载)的,需要一个简单的字符串。

One of the constructors (of which there are quite a few overloads) on the EF Data Context parent classes, takes a simple string.

这串是考虑到是在App或Web配置一个连接字符串使用的名称

This string is given to be the name of a connection string in the App or Web config to use.

您拨打电话是这样的:

using System.Data.Entity;

namespace MSSQL_EFCF.Classes
{
  public class DataAccess : DbContext
  {
    public DataAccess() : base("myConnectionString")
    {}

    public DbSet<MyTableObject> MyObjects { get; set; }

  }
}

您仍然可以把任何代码你需要为你自己的启动(如DB初始化函数调用)的构造函数中,所有一旦基地调用完成时调用。

You can still put any code you need for your own start-up (Such as DB Initializer calls) inside your constructor, and all that will get called once the base call completes.

这样做的好处事情这样的力量实体框架始终使用指定的连接字符串,绝不别的了。

The advantage of doing things this way forces entity framework to always use the named connection string and never anything else.

这之所以捕获不少开发商出来,为什么它运行了一个使用的LocalDB是看似简单的。

The reason this catches many developers out, and why it runs off an uses localdb is deceptively simple.

实体框架的DbContext在默认情况下将使用派生类作为数据库名的数据上下文的名称,如果无法找到合适的连接字符串由该名称的任何配置文件,使假设你在开发模式下的工作没有一个完整的后备数据存储。

The Entity Framework DbContext by default will use the name of the data context derived class as a database name, and if it can't find a suitable connection string in any config file by that name, makes the assumption that your working in development mode without a full backing data store.

在我上面的例子中,EF将审查应用程序和/或Web.config文件名为myConnectionString连接字符串

In my example above, EF would examine App and/or Web.config for a connection string called "myConnectionString"

一旦它使得这个发展决策,它知道的LocalDB将出席,因为这被用最新建立的安装视觉工作室,所以它会自动寻找一个连接,并随后在它使用的上下文名称的数据库填充它。

Once it makes this development decision, it knows that localdb will be present as this gets installed with the latest builds of visual studio, and so it will automatically seek out a connection and populate it with a db that follows the name of the context in which it's used.

我以前写的博客文章关于这个问题,你可以在这里找到:

I've previously written a blog post on the subject, which you can find here :

http://www.codeguru.com/columns/dotnet/entity-framework-code-first-simplicity.htm

注意:以上适用于您使用EF连接任何数据库,它是决定实际数据存储是什么/连接字符串

NOTE: The above applies to any database that you connect with using EF, it's the connection string that decides what/where the actual data store is.

这篇关于在.mdf文件实体框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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