none
Runtime EF 5 connection for Database on Pendrive/usb RRS feed

  • Question

  • We have WPF Desktop Application using EF5 and SQL Server Local DB Database on Pendrive .

    We want to select database during application startup. Since Drive (H:/G) for Pendrive is not fixed, so we can not hardcode it or save in Config file.

    How can we make the runtime connection in such situation.

    Thanks

    Manthan


    Sunday, December 15, 2013 3:09 PM

Answers

  • Not sure if I can answer as this has been moved but:

    A few ideas see http://stackoverflow.com/questions/6003822/how-to-detect-a-usb-drive-has-been-plugged-in; Have a factory class that builds a connection string perhaps by searching for a db file and this can be used by EF

    var configuration = new Configuration();
    configuration.TargetDatabase = new DbConnectionInfo("Server=MyServer;Database=MyDatabase;Trusted_Connection=True;","System.Data.SqlClient");

    The connection string would include the actual path. This Configuration could then be used by database migrations 

    var migrator = new DbMigrator(configuration);

    migrator.Update();

    Or more simply you could new up a Context and pass the connection as a string into the ctor and on call the base ctor with:

    public FooContext(string conn)
                : base(conn)//connection string name 
            {
            }
            

    Where you do this depends on how your app is structured eg use DI, MVVM etc?




    Monday, December 16, 2013 4:09 PM
  • Hello Manthan,

    >>How can we make the runtime connection in such situation?

    For this, we can add an additional constructor in YourContext() like below:

    public YourContext (string connectionstring)
    
                : base(connectionstring)
    
            {
    
            }
    

    Then we can use codes like below to call the YourContext() object:

    string connectionstring = "metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string='data source=(localdb)\\v11.0;initial catalog=DataBaseFirstDB;integrated security=True;multipleactiveresultsets=True;application name=EntityFramework'";
    
                using (YourContext db = new YourContext (connectionstring))
    
                {
    
                    Order order = db.Orders.Find(9);
    
                    db.Entry<Order>(order).State = EntityState.Deleted;
    
                    db.SaveChanges();
    
                }
    

    For the connectionstring, you can then spell it dynamic since it is a string type like:

    string connectionstring = "metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string='data source”+YourSever+”;catalog=”+YourDBFile+”;integrated security=True;multipleactiveresultsets=True;application name=EntityFramework'";

    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.

    Tuesday, December 17, 2013 3:44 AM
    Moderator

All replies

  • On application load find the drive letter and form the path.

    A.m.a.L Hashim
    Microsoft Most Valuable Professional
    My Blog - Dot Net Goodies

    Sunday, December 15, 2013 3:32 PM
  • Hi,

    I think you should post your question in the WPF forum, I will move this thread to it.

    Thanks.


    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.

    Monday, December 16, 2013 5:44 AM
  • Not sure if I can answer as this has been moved but:

    A few ideas see http://stackoverflow.com/questions/6003822/how-to-detect-a-usb-drive-has-been-plugged-in; Have a factory class that builds a connection string perhaps by searching for a db file and this can be used by EF

    var configuration = new Configuration();
    configuration.TargetDatabase = new DbConnectionInfo("Server=MyServer;Database=MyDatabase;Trusted_Connection=True;","System.Data.SqlClient");

    The connection string would include the actual path. This Configuration could then be used by database migrations 

    var migrator = new DbMigrator(configuration);

    migrator.Update();

    Or more simply you could new up a Context and pass the connection as a string into the ctor and on call the base ctor with:

    public FooContext(string conn)
                : base(conn)//connection string name 
            {
            }
            

    Where you do this depends on how your app is structured eg use DI, MVVM etc?




    Monday, December 16, 2013 4:09 PM
  • Hello Manthan,

    >>How can we make the runtime connection in such situation?

    For this, we can add an additional constructor in YourContext() like below:

    public YourContext (string connectionstring)
    
                : base(connectionstring)
    
            {
    
            }
    

    Then we can use codes like below to call the YourContext() object:

    string connectionstring = "metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string='data source=(localdb)\\v11.0;initial catalog=DataBaseFirstDB;integrated security=True;multipleactiveresultsets=True;application name=EntityFramework'";
    
                using (YourContext db = new YourContext (connectionstring))
    
                {
    
                    Order order = db.Orders.Find(9);
    
                    db.Entry<Order>(order).State = EntityState.Deleted;
    
                    db.SaveChanges();
    
                }
    

    For the connectionstring, you can then spell it dynamic since it is a string type like:

    string connectionstring = "metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string='data source”+YourSever+”;catalog=”+YourDBFile+”;integrated security=True;multipleactiveresultsets=True;application name=EntityFramework'";

    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.

    Tuesday, December 17, 2013 3:44 AM
    Moderator