How can I create a database schema dynamically using Entity Framework in Azure SQL Database?

Basically, I have a DBContext in my application, but my application will be multi-user, which means we need to create a separate database for each of our clients to isolate the data from each other.

I found this example for creating a DB on Azure https://code.msdn.microsoft.com/windowsazure/How-to-create-a-Azure-SQL-5157ce03

static void Main(string[] args) 
      { 

          SqlManagementClient client = new SqlManagementClient(getCredentials()); 
     //Server Name is your SQL Azure DataBase server name, for example:da0dt6hrt6 
          client.Databases.Create("{Server-Name}", new Microsoft.WindowsAzure.Management.Sql.Models.DatabaseCreateParameters() 
          { 
              Name = "OnecodeTest", 
              MaximumDatabaseSizeInGB = 1, 
              CollationName = "SQL_Latin1_General_CP1_CI_AS", 
              Edition="Web" 
          }); 

          Console.ReadLine(); 
      } 

      

However, I do not know how to programmatically create my CODE FIRST model in this new database.

this is my appdatacontext

public class AppDataContext : DbContext
    {

        public AppDataContext() : base("AppDataContext")
        {
        }

        public DbSet<Module> Modulos { get; set; }

        public DbSet<Empresa> Empresas { get; set; }

        public DbSet<Entidad> Entidades { get; set; }

        public DbSet<Propiedad> Propiedades { get; set; }

        public DbSet<ModulosPorUsuario> ModulosPorUsuario { get; set; }

        public DbSet<PerUserWebCache> PerUserCacheList { get; set; }


        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        }

    }

      

+3


source to share





All Articles