代码 - 首先更改不带连接字符串的数据源 [英] Code-First change data source without connection string

查看:88
本文介绍了代码 - 首先更改不带连接字符串的数据源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎有一个创建一个MVC4应用程序的问题,我采用了代码优先的方法来创建我的模型,但是没有连接字符串似乎是在web.config文件中创建的。



构建的数据库似乎已经建立在(localhost)\SQLEXPRESS实例上,但我想将其更改为外部数据源。没有要更新的连接字符串我不知道该怎么做。



请问有人可以指出我正确的方向吗?



编辑:我找到以下图表,突出了答案所说的相当好的



EF DB初始化流程图http://www.entityframeworktutorial.net/images/codefirst/database-init- fg1.PNG

解决方案

您必须自己添加一个连接字符串

 < add name =SomeDbconnectionString =Data Source = SERVENAME; Initial Catalog = DBNAME; User Id = loginid; Password = passwordproviderName =System.Data .SqlClient/> 

此字符串的名称应与上下文类的名称相匹配。

  public class SomeDb:DbContext 
{
public SomeDb()
:base(name = SomeDb)
{
}
}

应该这样做。 >

I seem to have an issue creating an MVC4 application where I have adopted the code-first approach to creating my models but no connection string seems to have been created in the web.config file.

The constructed database seems to have been built on (localhost)\SQLEXPRESS instance but I would like to change this to an external data source. Without a connection string to update I'm not sure how to do this.

Please could someone point me in the right direction?

EDIT: I found the following diagram which highlights what the answers have said pretty well

EF DB Initialization Flowchart http://www.entityframeworktutorial.net/images/codefirst/database-init-fg1.PNG

解决方案

You have to add a connection string yourself

<add name="SomeDb" connectionString="Data Source=SERVENAME;Initial Catalog=DBNAME;User Id=loginid;Password=password" providerName="System.Data.SqlClient" />

Name of this string should match to the name of your context class.

public class SomeDb: DbContext
{
    public SomeDb()
        : base("name=SomeDb")
    { 
    }
}

This should do it.

这篇关于代码 - 首先更改不带连接字符串的数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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