I installed FluentMigration to manage my SQL files.
In the package management I execute the following commands:
PM> dotnet add package FluentMigrator
PM> dotnet add package FluentMigrator.Runner
Migration
[Migration(201805041513)]
public class _201805041513_CriacaoTabelaPessoa : ForwardOnlyMigration
{
public override void Up()
{
Create.Table("Pessoa")
.InSchema("angularCore")
.WithColumn("Id").AsInt32().Identity()
.WithColumn("Nome").AsString(80)
.WithColumn("SobreNome").AsString(50)
.WithColumn("Email").AsString(50)
.WithColumn("IdTpoPessoa").AsInt16()
.WithColumn("IdEndereco").AsInt16();
}
}
Out-of-process (for some corporate requirements)
PM> dotnet tool install -g FluentMigrator.DotNet.Cli
Error:
No executable found corresponding to the "dotnet-tool" command
Documentation
Edit
Run in
PM> dotnet tool install -g FluentMigrator.DotNet.Cli
PM> dotnet fm migrate -p sqlite -c "Data Source=test.db" -a ".\bin\Debug\netcoreapp2.1\test.dll"
Generate teste.db
In the old versions you run the migrations directly in the database, I
did not understand how to update my database, ie create the Person
table through the generated test.db file?
I was able to get this to work without any issues. Here is what I did:
First I installed .NET Core 2.1-Preview 2. After installing I verified the version:
dotnet --version
2.1.300-preview2-008533
I then created the project
mkdir testfm
cd testfm
dotnet new console
Installed the nuget packages
dotnet add package FluentMigrator
dotnet add package FluentMigrator.Runner
dotnet add package FluentMigrator.Runner.SQLite
dotnet add package Microsoft.Data.Sqlite
Installed the CLI tool
dotnet tool install -g FluentMigrator.DotNet.Cli
Created a Migration Class called Migration1.cs
using FluentMigrator;
namespace test
{
[Migration(201805041513)]
public class _201805041513_CriacaoTabelaPessoa : ForwardOnlyMigration
{
public override void Up()
{
Create.Table("Pessoa")
.InSchema("angularCore")
.WithColumn("Id").AsInt32().Identity()
.WithColumn("Nome").AsString(80)
.WithColumn("SobreNome").AsString(50)
.WithColumn("Email").AsString(50)
.WithColumn("IdTpoPessoa").AsInt16()
.WithColumn("IdEndereco").AsInt16();
}
}
}
Compiled the project
dotnet build
Ran the migration from the root project directory
dotnet fm migrate -p sqlite -c "Data Source=test.db" -a
".\bin\Debug\netcoreapp2.1\test.dll"
I then Received the following messages.
I then confirmed the table was created by viewing the SqlLite DB
To run this same migration on a Sql Server 2016 you would run:
dotnet fm migrate -p SqlServer2016 -c
"server=SQLSERVERINSTANCE;uid=testfm;pwd=test;Trusted_Connection=yes;database=FluentMigrator"
-a ".\bin\Debug\netcoreapp2.1\test.dll"
Related
I am doing my first webAPI with .net and I am trying to implement the Clean Architecture in my project. But I do not know where should I put my AppDBContext. Here is my code.
using Microsoft.EntityFrameworkCore;
namespace ReactNoteAPI.Data
{
public class AppDBContext : DbContext
{
public DbSet<Note> Notes { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(#"Server=(localdb)\MSSQLLocalDB;Database=Notes;Trusted_Connection=True;");
}
}
}
echo# off
dotnet new mvc -o fqSSDb
cd fqSSDb
cls
echo dotnet add package Microsoft.EntityFrameworkCore.Sqlite
dotnet add package Microsoft.EntityFrameworkCore.Sqlite
REM echo "add package Microsoft.EntityFrameworkCore.SqlServer"
REM dotnet add package Microsoft.EntityFrameworkCore.SqlServer
cls
echo "dotnet add package Microsoft.VisualStudio.Web.CodeGeneration.Design"
dotnet add package Microsoft.VisualStudio.Web.CodeGeneration.Design
cls
echo "dotnet add package Microsoft.EntityFrameworkCore.SqlServer.Design"
dotnet add package Microsoft.EntityFrameworkCore.SqlServer.Design
cls
echo "dotnet add package Microsoft.EntityFrameworkCore.Tools"
dotnet add package Microsoft.EntityFrameworkCore.Tools
cls
echo "dotnet tool install --global dotnet-aspnet-codegenerator"
dotnet tool install --global dotnet-aspnet-codegenerator
cls
echo "Finally..."
dotnet ef dbcontext scaffold "Server=(localdb)\mssqllocaldb;Database=fqSSDb;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -o fqSSDb -c "fqSSDbContext" -d --context-namespace fqSSDb --context-dir fqSSDb
REM https://docs.microsoft.com/en-us/aspnet/core/fundamentals/tools/dotnet-aspnet-codegenerator?view=aspnetcore-6.0
REM Uninstall aspnet-codegenerator
REM It may be necessary to uninstall the aspnet-codegenerator to resolve problems. For example, if you installed a preview version of aspnet-codegenerator, uninstall it before installing the released version.
REM The following commands uninstall the dotnet-aspnet-codegenerator tool and installs the latest stable version:
REM Now easily you can generate CRUD code by applying the following command :
dotnet aspnet-codegenerator controller -name fqRequestController -m fqRequest -dc fqRequestDbContext --relativeFolderPath Controllers --useDefaultLayout --referenceScriptLibraries
REM note: but before run that command make sure you have made the necessary changes at your
REM - appsettings.json
REM - "ConnectionStrings": {"DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=fqSSDb;Trusted_Connection=True;MultipleActiveResultSets=true"}
REM - startup.cs file
REM var fqssdbConnectionString = builder.Configuration.GetConnectionString("fqSSDbConnection");
REM builder.Services.AddDbContext<FqSSDbContext>(options => options.UseSqlServer(fqssdbConnectionString));
We are using Azure DevOps pipelines. We have a step that publishes the our private nuget package to the Azure Artifacts. However, the build breaks at other steps because the nuget package (that was published on previous steps) is not found. The strange thing is that after the package is published, I can see it in the package-manager console or on Visual Studio and even in the Artifacts in Azure DevOps. But for some reason, the pipeline doesn't find the package. After 30-50min, I re-run the pipeline and then it finds the package.
What could be happening to take so long time for the pipeline to find the my package?
Edit 1:
This is my yaml for the step with error
- script: |
pwd && ls -la
dotnet restore "$(solution_path)" $(nuget_args)
dotnet publish -c Release -o $(System.DefaultWorkingDirectory)/bin "$(main_project_path)"
mkdir artifact
cp -r $(System.DefaultWorkingDirectory)/bin artifact/bin
displayName: Build Application
The error is:
/data/vstsagent/user/389/s/src/MyProject.csproj
: error NU1102: Unable to find package MyPackage with
version (>= 2.1.0) [/data/vstsagent/user/389/s/src/MySolution.sln]
/data/vstsagent/user/389/s/src/MyProject.csproj
: error NU1102: - Found 28 version(s) in MyPrivateRepository [ Nearest
version: 2.1.0-preview.6 ]
[/data/vstsagent/user/389/s/src/MySolution.sln]
It is not clear what feed is used by dotnet restore command.
Confirm that you have configured your custom nuget feed in nuget.config and using it.
Try adding "--verbose" switch in dotnet restore command and check if your feed is used for restoring packages.
Use the --no-cache option so dotnet restore doesn't use the local cache.
I am using asp.net core 2.1 with visual studio code or rider in mac. I already have a 2.1 sdk install on mac, while using the below command
dotnet-ef database update --project XXXX.XXXX
I get an exception as
zsh: command not found: dotnet-ef
Using the command
dotnet tool install --global dotnet-ef
getting an exception as Tool 'dotnet-ef' is already installed.
Then using this command dotnet tool restore
error NU3037: Package 'dotnet-ef 3.1.1' from source 'https://api.nuget.org/v3/index.json': The repository countersignature validity period has expired.
Package "dotnet-ef" failed to restore, due to Microsoft.DotNet.ToolPackage.ToolPackageException: The tool package could not be restored.
For mac I need to export the below path
export PATH="$PATH:$HOME/.dotnet/tools/"
Yes, give an example of the error:
$ dotnet ef
Could not execute because the specified command or file was not found.
Possible reasons for this include:
* You misspelled a built-in dotnet command.
* You intended to execute a .NET Core program, but dotnet-ef does not exist.
* You intended to run a global tool, but a dotnet-prefixed executable with this name could not be found on the PATH.
The second and third refer to dotnet trying to find a dotnet-ef command but it cannot find it. As the third point said, dotnet-ef is not on your way.
This is what the documentation says:
Global tools can be installed in the default directory or in a specific location. The default directory is:
OS Path
Linux/macOS $HOME/.dotnet/tools
Windows %USERPROFILE%\.dotnet\tools
So, you should add $HOME/.dotnet/tools/ to your $PATH.
For Linux and macOS, add a line to the shell configuration:
bash/ zsh:
export PATH="$PATH:$HOME/.dotnet/tools/"
When you start a new shell/terminal (or next time you log in) dotnet ef should work.
For Windows:
You need to add %USERPROFILE%.dotnet\tools to PATH.
I am using asp.net core 6.0
zhengguo#macdeMac-min % echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Applications/VMware Fusion.app/Contents/Public:/usr/local/share/dotnet:~/.dotnet/tools:/Library/Apple/usr/bin:/Library/Frameworks/Mono.framework/Versions/Current/Commands
The PATH has already exist ~/.dotnet/tools
When I use dotnet aspnet-codegenerator, it works. dotnet-aspnet-codegenerator not work.
I am trying to add scaffolding for a new page in my Razor Pages project. However, I keep getting the following output in the ASP.NET Core Scaffolder:
Checking if needed NuGet packages are already installed…
Building project…
Running /Users/lukeengland/Projects/AssetManager2/AssetManager2/obj/dotnet-aspnet-codegenerator --configuration "Debug" --project "/Users/lukeengland/Projects/AssetManager2/AssetManager2/AssetManager2.csproj" razorpage --model User --dataContext NewUserContext --referenceScriptLibraries --useDefaultLayout --no-build -outDir "/Users/lukeengland/Projects/AssetManager2/AssetManager2/Pages/Users2" --namespaceName AssetManager2.Pages.Users2 --useSqlite
Finding the generator 'razorpage'...
No code generators found with the name 'razorpage'
at Microsoft.VisualStudio.Web.CodeGeneration.CodeGeneratorsLocator.GetCodeGenerator(String codeGeneratorName)
at Microsoft.VisualStudio.Web.CodeGeneration.CodeGenCommand.Execute(String[] args)
RunTime 00:00:03.28
No code generators found with the name 'razorpage'
This happens when trying to create scaffolding using any model in my project. I was able create scaffolding for some pages a few weeks ago so not sure what has changed since then. Any suggestions greatly appreciated
P.S: I am using Microsoft Visual Studio 2019 on a Mac
I'm having exactly same error on VS Mac and version of 'dotnet-aspnet-codegenerator' and 'Microsoft.VisualStudio.Web.CodeGeneration.Design'. Instead of using VS wizard, using dotnet CLI seems able to work around and create all the scaffold files, but will need to see if it really works..Still learning:)
please try:
dotnet aspnet-codegenerator --configuration "Debug" --project "/Users/lukeengland/Projects/AssetManager2/AssetManager2/AssetManager2.csproj" razorpage --model User --dataContext NewUserContext --referenceScriptLibraries --useDefaultLayout --no-build -outDir "/Users/lukeengland/Projects/AssetManager2/AssetManager2/Pages/Users2" --namespaceName AssetManager2.Pages.Users2 --useSqlite
I had same issue on Ubuntu using CLI scaffold tool. The workaround for me was downgrading the SDK (and ) to 3.1.301 version as said in this github comment:
sudo apt install dotnet-sdk-3.1=3.1.301-1
dotnet tool install --global dotnet-aspnet-codegenerator --version 3.1.3
Same issue happened to me when trying to create a razor page EF scaffolding using visual studio for Mac with latest .NET 5.
Finding the generator 'razorpage'...
No code generators found with the name 'razorpage'
at Microsoft.VisualStudio.Web.CodeGeneration.CodeGeneratorsLocator.GetCodeGenerator(String codeGeneratorName)
at Microsoft.VisualStudio.Web.CodeGeneration.CodeGenCommand.Execute(String[] args)
RunTime 00:00:03.76
No code generators found with the name 'razorpage'
I was able to create the scaffolding by running the dotnet CLI
dotnet aspnet-codegenerator --configuration "Debug" --project "/Users/Max/Repos/VSProjects/WebApplication/WebApplication/WebApplication.csproj" razorpage --model Movie --dataContext WebApplication.Data.WebApplicationContext --referenceScriptLibraries --useDefaultLayout --no-build -outDir "/Users/Max/Repos/VSProjects/WebApplication/WebApplication/Models" --namespaceName WebApplication.Models --useSqlite
I've used yoman to generate an ASP.Net Core Web API application via the Visual Studio Code Editor. For reference, I followed this tutorial here.
The API works fine. However, I am trying to use EntityFramework Core Migrations with SQL Server. When I type the following into the Visual Studio Code Terminal:
Add-Migration MyDbInitialMigration
I get the following message:
'Add-Migration' is not recognized as an internal or external command, operable program or batch file.
I have the Microsoft.EntityFrameworkCore.Tools: 1.1.0-preview4-final dependency installed. I did this using the .Net Core Project Manager (Nuget) extension.
In Visual Studio 2015 this command works fine from the Package Manager Console.
I assume that using Visual Studio Code's Terminal is the problem. But does anyone know how I can use EF Core Migrations from within the VSCode editor itself?
Solution
Running the dotnet ef migrations add InitialCreate command yielded the following error:
No executable found matching command "dotnet-ef"
To solve this I needed to install the following dependency, And add it to the tools section:
Microsoft.EntityFrameworkCore.Tools.DotNet
The correct format to add a new migration is:
dotnet ef migrations add yourMigrationName
and to update database is:
dotnet ef database update
You need to add:
dotnet tool install --global dotnet-ef
Im working on Mac, so Ruby is installed by default. My EF commands required lots of extra parameters --project, --startup-project etc. This was a pain to type every time, so I used rake to make this easier.
In my project root, I added a file called rakefile with these contents:
desc "Add Migraion"
task :'add-migration' do
ARGV.each { |a| task a.to_sym do ; end }
puts ARGV[1]
sh "dotnet ef migrations add " + ARGV[1] + " --project MyProject.Data/MyProject.Data.csproj --startup-project MyProject.Web/MyProject.Web.csproj "
end
desc "Remove Migraion"
task :'remove-migration' do
ARGV.each { |a| task a.to_sym do ; end }
puts ARGV[1]
sh "dotnet ef migrations remove --project MyProject.Data/MyProject.Data.csproj --startup-project MyProject.Web/MyProject.Web.csproj"
end
desc "Update Database"
task :'update-database' do
ARGV.each { |a| task a.to_sym do ; end }
puts ARGV[1]
sh "dotnet ef database update --project MyProject.Data/MyProject.Data.csproj --startup-project MyProject.Web/MyProject.Web.csproj"
end
Then at the command line, I run these commands:
rake add-migration <migrationName>
rake remove-migration
rake update-database
First we need to add reference in *.csproj file in the following way
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.2" />
</ItemGroup>
in Bash/Command prompt
dotnet restore
after that
dotnet ef migrations add MyDbInitialMigration