在实体框架C#上配置连接字符串

时间:2014-09-12 17:06:15

标签: c# entity-framework

我无法使用我的连接字符串。 iv'e为我的实体创建了一个部分类

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

这是我的部分课程

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Win32;

namespace EnrollmentSystem.Data_Model
{
    public partial class EnrollmentDBEntities
    {
        public EnrollmentDBEntities(MyconString constring) 
        {
            Database.Connection.ConnectionString = constring.ToString();
        }


        public class MyconString
        {
            public string Server { get; set; }

            public string Database { get; set; }

            public string Username { get; set; }

            public string Password { get; set; }


            public static MyconString Instance
            {
                get
                {
                    return new MyconString
                    {
                        Server = Registry.GetValue(Variables.keyName, "Server", "NULL").ToString(),
                        Database = Registry.GetValue(Variables.keyName, "Database", "NULL").ToString(),
                        Username = Registry.GetValue(Variables.keyName, "Username", "NULL").ToString(),
                        Password = Registry.GetValue(Variables.keyName, "Password", "NULL").ToString(),
                    };
                }
            }

            public void Save()
            {
                Registry.SetValue(Variables.keyName, "Server", Server, RegistryValueKind.String);
                Registry.SetValue(Variables.keyName, "Database", Database, RegistryValueKind.String);
                Registry.SetValue(Variables.keyName, "Username", Username, RegistryValueKind.String);
                Registry.SetValue(Variables.keyName, "Password", Password, RegistryValueKind.String);
            }

            public override string ToString()
            {
                string connectionstring = new System.Data.EntityClient.EntityConnectionStringBuilder
                {
                    Metadata = "res//*/Data Model.EnrollmentDataModel.csdl|res://*/Data Model.EnrollmentDataModel.ssdl|res://*/Data Model.EnrollmentDataModel.msl",
                    Provider = "System.Data.SqlClient",
                    ProviderConnectionString = new System.Data.SqlClient.SqlConnectionStringBuilder
                    {
                        InitialCatalog = Server,
                        DataSource = Database,
                        UserID = Username,
                        Password = Password,
                        IntegratedSecurity = true,
                        MultipleActiveResultSets = true
                    }.ConnectionString
                }.ConnectionString;

                return connectionstring;
            }
        }
    }
}

测试后连接是连接但是,当我即将插入它时说数据库

未初始化与数据库的连接。

PS。 App.Config中

  <connectionStrings>
<add name="EnrollmentDBEntities" connectionString="metadata=&quot;res://*/Data Model.EnrollmentDataModel.csdl|res://*/Data Model.EnrollmentDataModel.ssdl|res://*/Data Model.EnrollmentDataModel.msl&quot;;provider=System.Data.SqlClient;provider connection string=&quot;data source=******;initial catalog=EnrollmentDB;user id=*****;password=****;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient"/>

有谁能告诉我这有什么问题?

我的另一种方式是通过App.Config进行设置,但最终应用程序配置是应用程序配置根据我的导师,这是不好的方法,他甚至没有告诉我正确的方法。这就是我发布在这里的原因。感谢

0 个答案:

没有答案