Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code First: Create Tables but not database

I have ready my model on code first EF and I try it on sql express and it works. But I have a problem translating it to a sql server: I don't have the permissions to recreate a database I can only add tables to an empty database.

I already see this answer but when I'm trying to replicate it I have some troubles with the context part:

public class DropCreateDatabaseTables : IDatabaseInitializer<Context> {

#region IDatabaseInitializer<Context> Members



public void InitializeDatabase(Context context)

I already put the reference to System.Data.Entity but that don't work and the Context class not is the referenced on System.Runtime.Remoting.Contexts

There is something wrong in the code? Or is a better solution with the last tools of EF?

EDIT:

Finally was:

DbContext:

public class PeopleContext: DbContext
{
    public DbSet<Person> People { get; set; }
    public DbSet<Adress> Adresses{ get; set; }


    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        // Add Entity type configuration classes
        modelBuilder.Configurations.Add(new PersonConfiguration());
        modelBuilder.Configurations.Add(new AdressConfiguration());

    }
}

Initializer:

  public class DropCreateDatabaseTables : IDatabaseInitializer<PeopleContext>
{

    public void InitializeDatabase(PeopleContextContext)
    {

      bool dbExists;

      using (new TransactionScope(TransactionScopeOption.Suppress))

      {

        dbExists = Context.Database.Exists();

      }

      if (dbExists)

      {       

        // remove all tables
          Context.Database.ExecuteSqlCommand("EXEC sp_MSForEachTable 'ALTER TABLE ? NOCHECK CONSTRAINT ALL'");
          Context.Database.ExecuteSqlCommand("EXEC sp_MSforeachtable @command1 = \"DROP TABLE ?\"");



        // create all tables

        var dbCreationScript = ((IObjectContextAdapter)Context).ObjectContext.CreateDatabaseScript();

        Context.Database.ExecuteSqlCommand(dbCreationScript);


        Context.SaveChanges();

      }

      else

      {

          throw new ApplicationException("No database instance");

      }

    }
}

Call:

class Program
{
    static void Main(string[] args)
    {
        var person= new Person
        {
            Identifier= "John Doe"
        };
        Database.SetInitializer(new DropCreateDatabaseTables());
        using (var context = new PeopleContext())
        {
            context.People.Add(person);
            context.SaveChanges();


        }
    }
}

Thanks Lukas Kabrt!

like image 434
rlartiga Avatar asked Oct 21 '22 22:10

rlartiga


1 Answers

The Context class in the example should be your DbContext class i.e. the class where you specify your DbSet<>s.

Example:

DbContext class

public class DataContext : DbContext {
    public DbSet<Customer> Customers { get; set; }
}

DatabaseInitializer

public class DropCreateDatabaseTables : IDatabaseInitializer<DataContext> {
    ...
}
like image 188
Lukas Kabrt Avatar answered Oct 30 '22 12:10

Lukas Kabrt