0

I am using ASP.NET MVC 4 Internet application template. I have successfully added some custom tables to the existing database that is created automatically but now I want to seed some data to one of my tables once database is created. I know I can do it by overriding the Seed method on the database initializer but as by default asp.net mvc4 sets the initializar to null I am not sure if I change the initializer can cause some new issues or side effects... how to do it?

// I could implement this custom initializer but as by default is set to null
// 
public class UsersContextSeedInitializer : CreateDatabaseIfNotExists<UsersContext>
{
    protected override void Seed(UsersContext context)
    {
       // Populate my table here
    }
}

    private class SimpleMembershipInitializer
    {
        public SimpleMembershipInitializer()
        {
            Database.SetInitializer<UsersContext>(null);

            // I could do it here or better below?: Database.SetInitializer(new UsersContextSeedInitializer());


            try
            {
                using (var context = new UsersContext())
                {
                    if (!context.Database.Exists())
                    {
                        // Create the SimpleMembership database without Entity Framework migration schema
                        ((IObjectContextAdapter)context).ObjectContext.CreateDatabase();
                    }
                }

            // better here?: Database.SetInitializer(new UsersContextSeedInitializer());

                WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true);
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException("The ASP.NET Simple Membership database could not be initialized. For more information, please see http://go.microsoft.com/fwlink/?LinkId=256588", ex);
            }
        }
    }

1 Answer 1

1

You should call the database initializer from your Global.asax as it needs to be called before the first use of the DbContext.

In John Papa's pluralsight course on SPA, the implementation he has is as follows, this is in his derived DbContext Class (note the comment):

// ToDo: Move Initializer to Global.asax; don't want dependence on SampleData
static CodeCamperDbContext()
{
    Database.SetInitializer(new CodeCamperDatabaseInitializer());
}

This question also supports this.

Sign up to request clarification or add additional context in comments.

3 Comments

I would like to seed data after database creation without touching the above code, for example, after line: ((IObjectContextAdapter)context).ObjectContext.CreateDatabase(); Is it possible to force a call to seed from there?
Also, applying what you said, what happens when the database does not exists? I want to seed data after it is created as well, I mean, only when database is created for first time.
I'm not sure to be honest, I haven't seen any examples of that (or tried it for that matter!) Another answer to a similar question refered to this blog: blog.longle.net/2012/09/25/…. Not sure if that helps?

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.