什么是代码中的连接字符串首先在mvc中 [英] what is connectionstring in code first in mvc

查看:116
本文介绍了什么是代码中的连接字符串首先在mvc中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好

i想在我的CodeFirst Mvc项目中使用sqlconnection。

我的connectionstring不在web.config中。

i见下面的内容在ProductContecxt中:



hello
i wanna use sqlconnection in my CodeFirst Mvc Project.
my connectionstring is not in web.config.
i see something like below in ProductContecxt:

public ProductContext()
:base(SharedDll.Connections.ConnectionString())



是否正确?



我该怎么用呢在我使用之前的连接字符串中。


is it correct?

how can i use it in my connection string like before i used.

//before:
 String connectionstring = "DataSource=.\SQLEXPRESS;"
                            + "Initial Catalog=DEMO;"
                            + "Integrated Security=True";





但是现在如何在connectionstring



but now how can use public ProductContext()... in connectionstring

推荐答案

中使用 public ProductContext()... 你可以使用如下(这是一个例子。



Global.asax.cs



You can use like below (this is a sample).

Global.asax.cs

protected void Application_Start()
 {
  DataCatalog.ConnectionString = ConfigurationManager.ConnectionStrings["PawLoyalty"].ConnectionString;
 }







Web.config






Web.config

<connectionStrings>
    <add name="PawLoyalty" connectionString="Server=.;database=PawLoyalty;Trusted_connection=true;pooling=true;MultipleActiveResultSets=True" providerName="System.Data.SqlClient" />
    </connectionStrings>





DataCatalog.cs < br $> b $ b



DataCatalog.cs

public class DataCatalog : DbContext
    {
        public static string ConnectionString { get; set; }

        public DataCatalog()
            : base(ConnectionString)
        {

        }

}







我希望这会对你有所帮助。




I hope this will help to you.


这篇关于什么是代码中的连接字符串首先在mvc中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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