4

After migration of my project to .NET Core 2.0, fresh install of Visual Studio 15.5 and .NET CORE sdk 2.1.2, I am having an error when trying to add a migration using EF Core.

C:\Projects\SQLwallet\SQLwallet>dotnet ef migrations add IdentityServer.
An error occurred while calling method 'BuildWebHost' on class 'Program'.
Continuing without the application service provider. Error: Parameter count mismatch.


Done. To undo this action, use 'ef migrations remove'

As a result an empty migration class is created, with empty Up() and Down() methods.

The program.cs looks like:

public class Program
{

    public static IWebHost BuildWebHost(string[] args, string environmentName)
    {...}

    public static void Main(string[] args)
    {


        IWebHost host;
        host = BuildWebHost(args, "Development");

Please advise. The migration worked fine while on Core 1.0. I have a IDesignTimeDbContextFactory implemented, and my DBContext class has a parameterless constructor, so it could not be the reason.

2
  • You can safely ignore the message. It will go away when issue #9076 is resolved.
    – bricelam
    Dec 8, 2017 at 0:10
  • The problem is that the migration is not generating properly, it is empty.
    – Glebby
    Dec 8, 2017 at 7:50

2 Answers 2

10

My solution is to pass Array to HasData function, not generic List. If you use List, try to convert array with ToArray function.

Here is an example:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
      var users = new List<User>();
      var user1 = new User() { Id = 1, Username = "user_1" };
      var user2 = new User() { Id = 2, Username = "user_2" };

      users = new List<User>() { user1, user2 };
      modelBuilder.Entity<User>().HasData(users.ToArray());
}
1
  • It worked for me, thank you. Any idea why it doesn't work with generic lists?
    – Tohid
    Aug 10, 2019 at 10:11
2

Take a look at the OnModelCreating method in your DbContext. Make sure it calls base.OnModelCreating(builder). It should look something like:

protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);            

        builder.Entity<T>().HasData(...);
    }

If you are using builder.Entity<T>().HasData(...) to seed your data, make sure you pass it a T[] and not an IEnumerable.

1
  • Very helpful: "If you are using builder.Entity<T>().HasData(...) to seed your data, make sure you pass it a T[] and not an IEnumerable"
    – JuanK
    Oct 15, 2018 at 7:41

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.