none
Create a copy database by DbContext with a different database name RRS feed

  • Question

  • Hello,

    In an application that use entity framework DbContext, i want to create different databases for each company that is created with the same entity model.

    How can i create copy databases using DbContext of entity model without using sql script?

    Thanks in advance.

    • Moved by Kristin Xie Friday, September 19, 2014 7:15 AM
    Thursday, September 18, 2014 5:38 PM

Answers

  • Hello e066377,

    If you use the code first approach, you could declare an additional constructor as:

    public Sample20140912(string connectionString)
    
                : base(connectionString)
    
            {
    
            }
    

    In your dbcontext class. And using code below to generate the database:

    string databaseName = "MyDatabase1";
    
                string connectionString = @"data source=(localdb)\v11.0;initial catalog=" + databaseName + ";integrated security=True;";
    
                using (Sample20140912 db = new Sample20140912(connectionString))
    
                {
    
                    //db.Database.CreateIfNotExists();
    
                    var result = db.ProductSeasons.ToList();
    
                }
    

    For different companies, you just need to provide other name.

    If you uses the database/model first approach, I do not know why you do not want use script because it is easier, you just need to select the Generate Database From Model and it would generate the complete script for you and just need to copy it and change the database name and execute it.

    Best Regards,

    Fred.


    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.

    • Marked as answer by e066377 Sunday, September 21, 2014 6:11 PM
    Friday, September 19, 2014 7:41 AM
    Moderator

All replies

  • Hi friend,

    Like your content mentioned, this case related to entity framework, So i moved your thread to  ADO.NET Entity Framework and LINQ to Entities forum for better support.           

    Have a nice day!

    Kristin


    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.

    Friday, September 19, 2014 7:17 AM
  • Hello e066377,

    If you use the code first approach, you could declare an additional constructor as:

    public Sample20140912(string connectionString)
    
                : base(connectionString)
    
            {
    
            }
    

    In your dbcontext class. And using code below to generate the database:

    string databaseName = "MyDatabase1";
    
                string connectionString = @"data source=(localdb)\v11.0;initial catalog=" + databaseName + ";integrated security=True;";
    
                using (Sample20140912 db = new Sample20140912(connectionString))
    
                {
    
                    //db.Database.CreateIfNotExists();
    
                    var result = db.ProductSeasons.ToList();
    
                }
    

    For different companies, you just need to provide other name.

    If you uses the database/model first approach, I do not know why you do not want use script because it is easier, you just need to select the Generate Database From Model and it would generate the complete script for you and just need to copy it and change the database name and execute it.

    Best Regards,

    Fred.


    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.

    • Marked as answer by e066377 Sunday, September 21, 2014 6:11 PM
    Friday, September 19, 2014 7:41 AM
    Moderator