0

I have a completed asp.net core 3.1 app created by VS 2019 named BulkyBook. It contains the main app layer (BulkyBook), DataAccess layer, Models layer and utility layer. It was a code first app. I copied the project without any changes to my local host server (Ubuntu 20.04 on VMware work station and MariaDB as database). I could run the app on server successfully :

1- Go to project directory (contains BulkyBook.csproj)

2-Run dotnet tool install --global dotnet-ef

3- Restart the server

4-Run dotnet ef update database

5- My data base and tables builded :)

Now, I published my app to a folder as below (using Vs 2019 on windows):

Publish method : File System

Target Framework : netcoreapp3.1

Target Runtime : Linux-x64

Now I have a published folder having no .csproj file !!

When I copy it to my local server I do the same again:

1- Delete the database created recently

2- Go to published directory (my app directory with lots of .dll and wwwroot folder and ... but no .csproj file)

3- Run dotnet ef update database

But now the following error appeard:

No project was found. Change the current working directory or use the --project option.

I do not know how to publish my project to include .csproj file or maybe something else that is required for update database Maybe good to say that I can run my published app in this directory with dotnet BulkyBook.dll, though in the browser showing an error of Unknown database/Apply migrations

This is my BulkyBook.csproj file in VS 2019 : (BulkyBook is main layer/ Application layer)

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>

    <TargetFramework>netcoreapp3.1</TargetFramework>
    <UserSecretsId>aspnet-BulkyBook-3AD31971---B2E8-</UserSecretsId>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference 

Include="EntityFrameworkProfiler" Version="5.0.5044" />
    <PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="3.1.3" />
    <PackageReference 
Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="3.1.3" />
    <PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="3.1.3" />
    <PackageReference 

Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.1.3" />
    <PackageReference 

Include="Microsoft.EntityFrameworkCore" Version="3.1.3" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="3.1.2" />
    <PackageReference 
Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.2" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.3">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.Extensions.Identity.Stores" Version="3.1.3" />
    <PackageReference 

Include="Microsoft.Extensions.Logging" Version="3.1.4" />
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1.3" />
    <PackageReference Include="NLog.Web.AspNetCore" Version="4.9.2" />
    <PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="3.1.1" />
    <PackageReference Include="System.Configuration.ConfigurationManager" Version="4.7.0" />
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\BulkyBook.DataAccess\BulkyBook.DataAccess.csproj"  />
    <ProjectReference Include="..\BulkyBook.Models\BulkyBook.Models.csproj" />
    <ProjectReference Include="..\BulkyBook.Utility\BulkyBook.Utility.csproj" />
  </ItemGroup>

  <ItemGroup>
    <Folder Include="wwwroot\images\product\" />
  </ItemGroup>
</Project>
ehsan_kabiri_33
  • 341
  • 6
  • 13

1 Answers1

0

dotnet publish creates a bunch of files so your application could be run on hosting system:

The dotnet publish command's output is ready for deployment to a hosting system (for example, a server, PC, Mac, laptop) for execution. It's the only officially supported way to prepare the application for deployment.

The command-line interface (CLI) tools for Entity Framework Core in turn are usually used to perform design-time development tasks and require your source code (including .csproj files) . Though it seems that there is a way to use it without the actual code, but in your case it will require installation of tools on hosting system which is an overkill (IMO).

There are multiple ways to update database for your hosting environment. Here are some of I used (but not limited to):

  1. Run dotnet database update from build server with corresponding app configuration (in your case your machine where your run publish)
  2. Generate SQL scripts via dotnet ef migrations script and run it by hand/some other tool on target database
  3. Apply migrations at runtime via calling DbContext.Database.Migrate(); at some point of application startup.
Guru Stron
  • 102,774
  • 10
  • 95
  • 132