开发者

How can I get my database to seed using Entity Framework CodeFirst?

开发者 https://www.devze.com 2023-03-12 16:49 出处:网络
The database is created successfully (as are the tables) but is not seeded. I have spent several hours and read tons of articles but have not been able to get it. Any suggestions?

The database is created successfully (as are the tables) but is not seeded. I have spent several hours and read tons of articles but have not been able to get it. Any suggestions?

On a side note, is it possible to call the initializer without having a reference to my DatabaseContext in the client?

I have included all the relevant code I could think of. If anything else would be helpful, please let me know.

Things I've Tried:

  1. I deleted my connection string (since it defaults to sqlexpress anyways, just the name changed)
  2. I changed DropCreateDatabaseIfModelChanges to DropCreateDatabaseAlways, still the same.

Edit: The really weird thing is it worked once, but I have no idea how or why it broke again. I am assuming connection strings, but who knows.

DatabaseInitializer.cs开发者_Go百科

public class DatabaseInitializer : DropCreateDatabaseIfModelChanges<DatabaseContext>
{
  protected override void Seed(DatabaseContext context)
  {
    // Seeding data here
    context.SaveChanges();
  }
}

DatabaseContext.cs

public class DatabaseContext : DbContext
{
  protected override void OnModelCreating(DbModelBuilder mb)
  {
    // Random mapping code
  }

  public DbSet<Entity1> Entities1 { get; set; }
  public DbSet<Entity2> Entities2 { get; set; }

}

Global.asax.cs - Application_Start()

protected void Application_Start()
{
  Database.SetInitializer<DatabaseContext>(new DatabaseInitializer());
  AreaRegistration.RegisterAllAreas();
  RegisterGlobalFilters(GlobalFilters.Filters);
  RegisterRoutes(RouteTable.Routes);
}

Client web.config

<connectionStrings>
  <add name="DatabaseContext" connectionString="data source=.\SQLEXPRESS;Database=Database;Integrated Security=SSPI;" providerName="System.Data.SqlClient" />
</connectionStrings>

SOLUTION

For the sake of documentation, I am sharing my solution here. Navigating all the comments would be a pain anyways. In the end I had DatabaseInitializer and DatabaseContext in separate classes. I don't really understand while these tiny changes fixed it, but here it is.

DatabaseInitializer.cs

public class DatabaseInitializer : CreateDatabaseIfNotExists<DatabaseContext>
{
  protected override void Seed(DatabaseContext context)
  {
    // Seed code here
  }
}

DatabaseContext.cs

public class DatabaseContext : DbContext
{
  public DatabaseContext() : base("MyDatabase") { }

  protected override void OnModelCreating(DbModelBuilder mb)
  {
    // Code here
  }

  public DbSet<Entity> Entities { get; set; }
  // Other DbSets
}

Global.asax.cs - Application_Start()

protected void Application_Start()
{
  Database.SetInitializer(new DatabaseInitializer());
  AreaRegistration.RegisterAllAreas();
  RegisterGlobalFilters(GlobalFilters.Filters);
  RegisterRoutes(RouteTable.Routes);
}


This is what my DbContext classes all look like and they seed just fine:

public class MyDbContext : DbContext
{
    public DbSet<MyClass> MyClasses { get; set; }

    protected override void OnModelCreating (DbModelBuilder modelBuilder)
    {
        base.OnModelCreating (modelBuilder);
        modelBuilder.Conventions.Remove<System.Data.Entity.ModelConfiguration.Conventions.PluralizingTableNameConvention> ();

        // Add any configuration or mapping stuff here
    }

    public void Seed (MyDbContext Context)
    {
        #if DEBUG
        // Create my debug (testing) objects here
        var TestMyClass = new MyClass () { ... };
        Context.MyClasses.Add (TestMyClass);
        #endif

        // Normal seeding goes here

        Context.SaveChanges ();
    }

    public class DropCreateIfChangeInitializer : DropCreateDatabaseIfModelChanges<MyDbContext>
    {
        protected override void Seed (MyDbContext context)
        {
            context.Seed (context);

            base.Seed (context);
        }
    }

    public class CreateInitializer : CreateDatabaseIfNotExists<MyDbContext>
    {
        protected override void Seed (MyDbContext context)
        {
            context.Seed (context);

            base.Seed (context);
        }
    }

    static MyDbContext ()
    {
        #if DEBUG
        Database.SetInitializer<MyDbContext> (new DropCreateIfChangeInitializer ());
        #else
        Database.SetInitializer<MyDbContext> (new CreateInitializer ());
        #endif
    }
}

I have used this pattern a few times and it has worked out very well for me.


My Seed method was not invoked even with proper call to Database.SetInitializer in Application_Start... The reason for it was really simple: initializer may not be invoked at all if you don't yet have any code that actually uses database context.


This is my sad little tale.

First, lessons learned:

  1. The seed method won't be called until the context is used.
  2. The Global.asax.cs won't hit a breakpoint on first run bc it runs before the debugger is attached. To hit a breakpoint on Global.asax.cs, you have can add some white space to Web.config and hit a page; then it will get hit.
  3. If there are VS connections to the db, the seeding won't happen. The app will throw an error.

So, to avoid the sadness:

  • Disconnect your VS connection.
  • Switch the base class DropCreateDatabaseAlways for one go.
  • Hit a page that uses the context.

Now, the sadness:

  1. I had my custom Initializer class in my Global.asax.cs file. I had a break point on my Initializer Seed method; I started the application and the method never got hit. :(
  2. I point a break point in my Database.SetInitializer call in Application_Start. That never got hit. :(
  3. I realized that I had no db schema changes, so then I changed DropCreateDatabaseIfModelChanges to DropCreateDatabaseAlways. Still, nothing. :(
  4. I finally went to a page that uses the context, and it worked. :/


You can call update-database to manually run the seed method inside the Configuration class. This requires enable-migrations to be on as well.

PM> update-database
Specify the '-Verbose' flag to view the SQL statements being applied to the target database.
No pending code-based migrations.
Running Seed method.

internal sealed class Configuration : DbMigrationsConfiguration<ProjectManager.Data.Database.ProjectDb>
{
    public Configuration()
    {
        AutomaticMigrationsEnabled = false;
    }

    protected override void Seed(ProjectManager.Data.Database.ProjectDb context)
    {
        context.Status.AddOrUpdate(
            new Status() { Id = 1, Text = "New" },
            new Status() { Id = 2, Text = "Working" },
            new Status() { Id = 3, Text = "Completed" },
            new Status() { Id = 4, Text = "Skipped" }
        );
    }
}


The following change in the Global.asax file worked for me:

Old Code:

    protected void Application_Start()
    {
        Database.SetInitializer<mycontextclassname>(new DropCreateDatabaseAlways<mycontextclassname>());             
       ...
    }

New Code:

    protected void Application_Start()
    {
        Database.SetInitializer<mycontextclassname>(new DropCreateDatabaseAlways<mycontextclassname>()); 
        Database.SetInitializer(new Initializer()); 
        ...
    }


I too have had difficulty getting Seed() to be invoked. And I do appreciate all the helpful suggestions above and have had some luck using DropCreateDatabaseAlways ... but not ALWAYS!!

Most recently, I added the following line of code in the the constructor of my Repository to good effect:

    public CatalogRepository()
    {
        _formCatalog.FormDescriptors.GetType();

    }

It was sufficient to trigger the Seed() getting invoked. If you've tried everything above this answer and still no luck, give it a try. Good luck this was really a time consuming experience.


The seed event in your example will only be fired once as your using DropCreateDatabaseIfModelChanges you can change this to DropCreateDatabaseAlways i think and it should fire the seed event every time.

Edit

This is my DataContext

public WebContext()
{   
    DbDatabase.SetInitializer(new DropCreateDatabaseIfModelChanges<WebContext>());
}


This just happened to me while I was discovering Code First Features. This situation often happens when you have first used Code First to generate your database without any initialization strategy.

If you decide to do so later on by implementing a DropCreateDatabaseIfModelChanges based strategy, but without modifying you model, then your Seed method won't be called since the database generation and your strategy will only be applied next time you change your model.

If this happens to you, just try to modify your model a bit to test this hypothesis and I bet, your database is going to be populated ;)

I don't have the solution yet, except using a strategy that always generate the database, but I'm really not confortable with the fact of putting the initialization strategy in your DbContext since this class is goind to be used in you production environement, althought the initialization strategy seems to be mostly used for fluent developement environnement.


I've just come across this problem. I've deleted the "connectionstrings" section from the Web.config file, and currently the app started running - without the connectionstrings section! I add the section back, and the database is not seeding again. It's not a proper solution, but I'm just adding a data point here to what can potentially solve the problem.

Fortunately it's just a small "throwaway" app I'll discard soon anyway ...


Meticulously ensure that you didn't declare your context variable more than once. If you declare it again after seeding, the seed will be overwritten.


I was having same problem and after change in both Global.asax file and Intializer file it worked. I hope it will work for those who are still having problem for data seeding.

New Code in Global.asax:

    protected void Application_Start()
    {
        Database.SetInitializer<mycontextclassname>(new DropCreateDatabaseAlways<mycontextclassname>()); 
        Database.SetInitializer(new Initializer()); 
        ...
    }

code for Intializer file:

public class Initializer : System.Data.Entity.DropCreateDatabaseAlways<Context>


Updated to note that this answer is incorrect! The reason for my DB not getting seeded remains a mystery (but it wasn't the lack of a default base constructor call, as noted by @JaredReisinger)

I appreciate this question is a little old but I ended up here so someone else might. Here's my tuppence worth:

My DB was getting created fine but not seeded, even if I deleted the database and started again using DropDatabaseInitialiser.

After reading the code above I noticed that my context constructor was this

public MyApp_Context()
{
    // some code
}

whereas the example above would be as follows for my setup

public MyApp_Context() : base("name=MyApp_Context")
{
    // some code
}

Yup, I wasn't calling the base object's constructor! I wouldn't have expected everything except seeding to work in this instance but that appears to be the (repeatable) case.

NB, I don't actually need to supply the context name in the base constructor call; I only wrote it that way initially because I was copying the format of the solution above. So my code is now this, and seeding works on initial database creation.

public MyApp_Context() : base()
{
    // some code
}
0

精彩评论

暂无评论...
验证码 换一张
取 消