dotnet / efcore

EF Core is a modern object-database mapper for .NET. It supports LINQ queries, change tracking, updates, and schema migrations.
https://docs.microsoft.com/ef/
MIT License
13.69k stars 3.17k forks source link

Add-Migration on any dotnetcore project #7905

Closed ojorma closed 1 year ago

ojorma commented 7 years ago

I get an error when I to run add-migration with entitcore on any dotnetcore based project but the equivalent command works when targeting the full dotnetframework

Add-Migration : Exception calling "AddFromFile" with "1" argument(s): "'basePath' cannot be an empty string ("") or start with the null character. Parameter name: basePath" At line:1 char:1

see a snapshot of my code. It's a dotnetcore console app. Thesame will work if created the project based on dotnetframewok 4.6.1

Steps to reproduce

entitycorebug

Further technical details

Database Provider: (e.g. Microsoft.EntityFrameworkCore.SqlServer) Operating system: IDE: (e.g. Visual Studio 2017)

bricelam commented 7 years ago

What version of Microsoft.EntityFrameworkCore.Tools are you using? Does -Verbose give additional information?

ojorma commented 7 years ago

@bricelam thanks for the response. I figured it out and posted the answer here https://github.com/aspnet/EntityFramework/issues/7540#issuecomment-286636108

dvdobrovolskiy commented 7 years ago

Seems like https://github.com/aspnet/EntityFrameworkCore/issues/7540#issuecomment-286636108 does not work in my case

Suddenly got add-migration : Exception calling "AddFromFile" with "1" argument(s): "'basePath' cannot be an empty string ("") or start with the null ch aracter. after adding one new field to existing model.

petersgiles commented 7 years ago

I'm getting this as well

add-migration : Exception calling "AddFromFile" with "1" argument(s): "'basePath' cannot be an empty string ("") or start with the null character.
PM> dotnet --version
2.0.0

not at all sure how to proceed

petersgiles commented 7 years ago

I think I have worked something out.

  • I have my Data Model in a netstandard2.0 class library
  • I have a MVC (whatever the latest core 2.0 one is) app that I am using as the startup app required by EF Core 2.0. I'm mostly just scaffolding my data context in the MVC app.
  • dotnet --version is 2.0
  • I run add-migration Initial from the Package Manager Console
  • When I add a migration I get the "AddFromFile" error

What works though is adding a core 2.0 console app to the solution (not editing the code) and setting that as my startup project just before running the add-migration command.

So it would seem that there's some code in the web app that gets run when the app-mrigations command is issued.

I'm guessing its the Main method...here's mine

    public class Program
    {
        public static void Main(string[] args)
        {
            var host = BuildWebHost(args);

            using (var scope = host.Services.CreateScope())
            {
                var services = scope.ServiceProvider;
                try
                {
                    var context = services.GetRequiredService<AdminDataContext>();
                    DbInitializer.Initialize(context);
                }
                catch (Exception ex)
                {
                    var logger = services.GetRequiredService<ILogger<Program>>();
                    logger.LogError(ex, "An error occurred while seeding the database.");
                }
            }

            host.Run();
        }

        public static IWebHost BuildWebHost(string[] args)
        {
            return WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>()
                .Build();
        }
    }

I can only assume there's something path-y going on when constructing the host that isn't working from the package manager console.

I hope this gives someone who knows better a hint as to what is going on with this.

EDIT:

It looks like something I'm doing when building my container in the Startup class's ConfigureServices method

rsandhumcr commented 7 years ago

I had the same problem with .net core 1.1 . I solved my problem by creating a console app in my solution and make it start up project. Made a reference to the project containing my written db context in the console app. Executed the 'Add-Migration Initial' with success.

gharajedaghi commented 5 years ago

Add-Migration Initial with success.