Set Entity Framework connection string at runtime

后端 未结 2 1858
终归单人心
终归单人心 2021-01-15 23:28

I have generated entity model from AdventureWorks database; now I want to delete the connection string in app.config and set it at runtime. In the Model1.Context.cs file I h

相关标签:
2条回答
  • 2021-01-15 23:33

    I'd go with something like:

     public AdventureWorksEntities(string server, string databaseName, string user, string password)
            :base(new System.Data.EntityClient.EntityConnectionStringBuilder
            {
                Metadata = "res://*",
                Provider = "System.Data.SqlClient",
                ProviderConnectionString = new System.Data.SqlClient.SqlConnectionStringBuilder 
                {
                    InitialCatalog = databaseName,
                    DataSource = server,
                    IntegratedSecurity = false,
                    UserID = user,
                    Password = password,
    
                }.ConnectionString
            }.ConnectionString) 
        {
    
        }
    
    0 讨论(0)
  • 2021-01-15 23:49

    This is an example using standard .aspx login information to set the UserID and Password information in the connection string. No connection string settings are stored in the web.config or app.config file.

    Modify the Model.Designer.cs page as follows:

    public partial class Entities : ObjectContext
    {
        #region Constructors
    
    
        public static string getConStrSQL(string UID,string PWD)
        {
    
            string connectionString = new System.Data.EntityClient.EntityConnectionStringBuilder
            {
                Metadata = "res://*",
                Provider = "System.Data.SqlClient",
                ProviderConnectionString = new System.Data.SqlClient.SqlConnectionStringBuilder
                {
                    InitialCatalog = "your_database_name",
                    DataSource = "your_server",
                    IntegratedSecurity = false,
                    UserID = UID,                
                    Password = PWD,              
                }.ConnectionString
            }.ConnectionString;
    
            return connectionString;
        }
    
        /// <summary>
        /// Initialize a new Entities object.
        /// </summary>
        public Entities(string UID,string PWD)
            : base(getConStrSQL(UID,PWD), "Entities")
        {
            this.ContextOptions.LazyLoadingEnabled = true;
            OnContextCreated();
        }
        ......
    

    Then in your code behind page:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.Mvc;
    using System.Web.Security;
    
    
    public partial class views_html_form : System.Web.UI.Page
    {
    public void Page_Load()
    {
        if (currentUser() == null)
        {
            HttpContext.Current.Response.Redirect("~/login.aspx");
        }
    }
    public static MembershipUser currentUser()
    {
        MembershipUser currentUser = Membership.GetUser();
        return currentUser;
    }
    
    public static string UID()
    {
        string UID = currentUser().UserName;
        return UID;
    }
    public static string PWD()
    {
        string PWD = currentUser().GetPassword();
        return PWD;
    }
    public static void SelectRecord()
    {
        YourModel.Entities db = new YourModel.Entities(UID(), PWD());
        var query = from rows in db.Table_Name orderby rows.ID select rows;
       .....
    

    That's it. No messing around with .config files. Alternatively you could send a database name, for example, as a parameter in the same way.

    0 讨论(0)
提交回复
热议问题