none
WPF, Entity Framework, Conection String RRS feed

  • Question

  • Hello guys,

    I have a code:

    EntityConnectionStringBuilder efb = new EntityConnectionStringBuilder(ConfigurationManager.ConnectionStrings["AutoLotEntities"].ConnectionString);
    SqlConnectionStringBuilder newBuilder = new SqlConnectionStringBuilder(efb.ProviderConnectionString);
    newbuilder.UserID = "new_user";
    sqlcsBuilder.Password = "new_pass";
    so, I have got connection string and changed therein userId and password.

    now how should I upgrade/save connection string in app.config?

    • Moved by Yang,Chenfei Wednesday, January 29, 2014 1:32 AM EF related!
    Tuesday, January 28, 2014 6:14 PM

Answers

  • Hello,

    >>now how should I upgrade/save connection string in app.config?

    Is it that you want to use the new connection string to connect database? If it is, we do not need to. In your context class, add such a constructor:

     public DataBaseFirstDBEntities(DbConnection objectContext, bool dbContextOwnsObjectContext)

                : base(objectContext, dbContextOwnsObjectContext)

            {

            }

    After modifying the connection string, we can use codes as below to connect model:

    EntityConnectionStringBuilder efb = new EntityConnectionStringBuilder(ConfigurationManager.ConnectionStrings["WinFormDBEntities"].ConnectionString);
    
                efb.Metadata = "res://*/Model2.csdl|res://*/Model2.ssdl|res://*/Model2.msl";
    
                SqlConnectionStringBuilder newBuilder = new SqlConnectionStringBuilder(efb.ProviderConnectionString);
    
                newBuilder.InitialCatalog = "DataBaseFirstDB1";
    
                efb.ProviderConnectionString = newBuilder.ConnectionString;
    
    
                EntityConnection connection = new EntityConnection(efb.ConnectionString);
    
    
                using (DataBaseFirstDBEntities db = new DataBaseFirstDBEntities(connection,true))
    
                {
    
                    string strNull = null;
    
                    var result = from od in db.OrderDetails
    
                                 where (od.OrderDetailCode == "1" && od.OrderDetailName == null) || (od.OrderDetailCode == "2" && od.OrderDetailName == "2")
    
                                 select od;
    
    
                }
    

    My two different connection strings in app.config file:

    <add name="WinFormDBEntities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=(localdb)\v11.0;initial catalog=WinFormDB;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
    
        <add name="DataBaseFirstDBEntities" connectionString="metadata=res://*/Model2.csdl|res://*/Model2.ssdl|res://*/Model2.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=(localdb)\v11.0;initial catalog=DataBaseFirstDB;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
    

    The WinFormDBEntities connection string is used to connect a winform model and I read it form app.config and modified it to connect DataBaseFirstDBEntities model.

    If I misunderstand, please let me know.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, January 29, 2014 8:41 AM
    Moderator

All replies

  • Dear Guys,

    This issue is more related to Entity Framework, I will move this thread to Entity Framework Forum where you can contact many experts about this topic.

    Thanks for your understanding!

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, January 29, 2014 1:31 AM
  • Hello,

    >>now how should I upgrade/save connection string in app.config?

    Is it that you want to use the new connection string to connect database? If it is, we do not need to. In your context class, add such a constructor:

     public DataBaseFirstDBEntities(DbConnection objectContext, bool dbContextOwnsObjectContext)

                : base(objectContext, dbContextOwnsObjectContext)

            {

            }

    After modifying the connection string, we can use codes as below to connect model:

    EntityConnectionStringBuilder efb = new EntityConnectionStringBuilder(ConfigurationManager.ConnectionStrings["WinFormDBEntities"].ConnectionString);
    
                efb.Metadata = "res://*/Model2.csdl|res://*/Model2.ssdl|res://*/Model2.msl";
    
                SqlConnectionStringBuilder newBuilder = new SqlConnectionStringBuilder(efb.ProviderConnectionString);
    
                newBuilder.InitialCatalog = "DataBaseFirstDB1";
    
                efb.ProviderConnectionString = newBuilder.ConnectionString;
    
    
                EntityConnection connection = new EntityConnection(efb.ConnectionString);
    
    
                using (DataBaseFirstDBEntities db = new DataBaseFirstDBEntities(connection,true))
    
                {
    
                    string strNull = null;
    
                    var result = from od in db.OrderDetails
    
                                 where (od.OrderDetailCode == "1" && od.OrderDetailName == null) || (od.OrderDetailCode == "2" && od.OrderDetailName == "2")
    
                                 select od;
    
    
                }
    

    My two different connection strings in app.config file:

    <add name="WinFormDBEntities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=(localdb)\v11.0;initial catalog=WinFormDB;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
    
        <add name="DataBaseFirstDBEntities" connectionString="metadata=res://*/Model2.csdl|res://*/Model2.ssdl|res://*/Model2.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=(localdb)\v11.0;initial catalog=DataBaseFirstDB;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
    

    The WinFormDBEntities connection string is used to connect a winform model and I read it form app.config and modified it to connect DataBaseFirstDBEntities model.

    If I misunderstand, please let me know.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, January 29, 2014 8:41 AM
    Moderator