6

I am building a simple MVC application for managing a library. For development purposes, I would like the EF to drop and recreate the database everytime the model changes, as well as filling it with some sample data. At this moment I struggle at getting the initializer to work. The Initializer class looks like this:

public class LibraryInitializer : DropCreateDatabaseIfModelChanges<LibraryContext>
{
    protected override void Seed(LibraryContext context)
    {
        // sample data to be writted to the DB
    }
}

And the context class looks like this:

public class LibraryContext : DbContext
{
    public DbSet<User> Users { get; set; }
    public DbSet<Book> Books { get; set; }

    public LibraryContext()
    {
        Database.SetInitializer<LibraryContext>(new LibraryInitializer());
    }
}

At this moment I get the following error:

Member 'Database.SetInitializer(IDatabaseInitializer)' cannot be accessed with an instance reference; qualify it with a type name instead

Based on many guides available on the Web, this is the way to use the initializer, but I have no idea why this error occurs. Any help would be greatly appraciated. Thank you!

1 Answer 1

24

In C# you can't access static members from instances. You must access static members using the name of the type. Use full type name.

 System.Data.Entity.Database.SetInitializer<LibraryContext>(new LibraryInitializer());

DbContext has a Database property which is an instance of System.Data.Entity.Database class. When you write Database in your DbContext it points to that instance and since you can not access static methods from instances you get the error.

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

4 Comments

Thank you for the answer, that pretty much solved the problem. Well, the thing is, when I check any tutorials/blog posts explaining how to build initializers (don't wanna point to any specific website), none of them actually show the full System.Data.Entity.Database.SetInitializer.. name. Is it something with my code that forces to use it or it's not mentioned anywhere as "everyone should know that"?
I had the same issue recently, and I had my database classes in a folder called Database, which caused the namespace to look like MyService.Database. In that case I had to fully specify System.Data.Entity.Database otherwise, you would not have to do that.
you need to add using System.Data.Entity; into your using directions - otherwise it will try to use the Database property of your DbContext class, because it can't find the class.
I got the error evening when adding a System.Data.Entity using directive. Only by fully qualifying the type as dotctor explains did I solve the problem.

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.