5

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!

2
  • Don't start by writing your own initializer, but use the standard ones, like Database.SetInitializer(new DropCreateDatabaseAlways<Context>()); See DropCreateDatabaseAlways. Commented Oct 14, 2013 at 21:32
  • Because my credentials I can't drop the database and recreate it I just can create tables over an existing empty database Commented Oct 14, 2013 at 23:56

1 Answer 1

3

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> {
    ...
}
Sign up to request clarification or add additional context in comments.

3 Comments

I try your solution, but I don't know how it does nothing: Can I edit my post with the code and you tell me if is correct?
Go ahead, I can try looking at your ... I though you have just problems with compiling your code, so I haven't written any implementation of the initializer
Don't worry it work I edited de question with the correct code. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.