EF migration in VScode - entity-framework

I tried to do a migration with dotnet ef migrations add InitialCreate and I obtained the message :
Build started...
Build succeeded.
System.ArgumentNullException: Value cannot be null. (Parameter 'connectionString')
appsettings.json
"ConnectionsStrings":{
"DefaultConnection":"Data Source=(localdb)\LocalDBApp1;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"
},
Startup.cs
services.AddDbContext(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
Have you an idea how I can solve this problem?
Thanks in advance

Probably a typo, try with ConnectionStrings instead of ConnectionsStrings in your appsettings.json.

Related

.NET Core EF Scaffolding throws unable to find provider assembly 'Source=localhost'

I'm using core 3 and I have included below packages to my project
"Microsoft.EntityFrameworkCore.Design": "5.0.7",
"Microsoft.EntityFrameworkCore.SqlServer": "5.0.7",
"Microsoft.EntityFrameworkCore.Tools": ""5.0.7""
And here is my connection string and I have tested the connection before and it succeeded
"ConnectionStrings": { "bikeStore": "Data Source=localhost;Initial Catalog=BikeStore;User ID=sa;Password=***********" }
Now I want to run the Scaffold command as below
Scaffold-DbContext Data Source=localhost;Initial Catalog=BikeStore;User ID=sa;Password=***********Microsoft.EntityFrameworkCore.SqlServer -OutputDir Data
And I got below error
Build started...
Build succeeded.
Unable to find provider assembly 'Source=localhost'. Ensure the name is correct and it's referenced by the project.
Here's the Server Explorer and how I connected to my db
What is wrong with my approach?
You can replace Data Source with Server and provide the provider flag:
Scaffold-DbContext -Connection "Server=localhost;Initial Catalog=BikeStore;User ID=sa;Password=***********;" -Provider Microsoft.EntityFrameworkCore.SqlServer -OutputDir Data
Microsoft.EntityFrameworkCore 3.1.31
Microsoft.EntityFrameworkCore.SqlServer 3.1.31
Microsoft.EntityFrameworkCore.Tools 3.1.31
dotnet ef Scaffold-DbContext "Server=.; Initial Catalog=CourseDB;User ID=sa; Password=sa;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models

FluentMigrator rollback not working

I see the related question already there. FluentMigrator Failed Migrations Don't Rollback? and Rollback to a specfic Migration in FluentMigrator. But unfortunately i can't solve my rollback issue with this solution. I am using FluentMigrator to versioning database.
My migration code :
using FluentMigrator;
namespace WebCruiter.Candidate.DBMigration.Migrations.R2016_6
{
[Migration(20160908000908, "USERSTORY")]
public class Migration20160908000908 : AutoReversingMigration
{
public override void Up()
{
Create.Column("TestUrl").OnTable("JobApplication").AsString(500).Nullable();
}
}
}
And my attempt to rollback this version(20160908000908) from command line:
migrate.exe -c "server=(LocalDB)\MSSQLLocalDB;Initial Catalog=Candidate;Integrated Security=True" -db sqlserver2014 -a ".\..\..\..\WebCruiter.Candidate.DBMigration\bin\Debug\FluentMigrator.dll" -t rollback:20160908000908
Without rollback column TestUrl from JobApplication it shows :
Can anybody help me out where i made a mistake ?
Because you need to give the runner the number of the migration before the one you want to rollback to. So say you have migrations 1, 2, and 3. And you want to rollback 3, you would give the runner 2
What you are giving the runner at the moment is not that, give it the migration before '20160908000908'.
This is basically what was written in Rollback to a specfic Migration in FluentMigrator, You write the number of the migration you want as your last one, not the number of migration you want to rollback to.

Reverse Engine of Entity Framework Core

Greetings i have an existing database and i was going to build my application using ASP.NET Core, based on this tutorial, i've installed the packages for Entity Framework core, now i have to reverse my tables, so as tutorial said i used this command:
Scaffold-DbContext "Server=
(localdb)\mssqllocaldb;Database=RFID;Trusted_Connection=True;"
Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models
However it keeps getting me this error:
Invalid JSON file in C:\Users\user11\documents\visual studio
2015\Projects\CoreOAS\src\CoreOAS\project.json
what should i do?
If you have a comment like below then above error will emit.So you have to remove that.
Wrong :
// required for EF <--- this is the issue
"buildOptions": {
"emitEntryPoint": true
},
Correct way :
"buildOptions": {
"emitEntryPoint": true
},
You can read more about it here : their (JSON) decision not to support comments

Entity Framework code first - update database can't find .mdf in project

I am creating a small app with EF code first and I want to call the update database command from the package manager console and target the database in my app.config which is a local .mdf.
I have this connection string in my app.config
<connectionStrings>
<add name="EventsConnectionString"
connectionString="Data Source=(localdb)\v11.0;Database=EventsApp;Trusted_Connection=Yes;" />
</connectionStrings>
RegistrantContext class with connection string in constructor
using System.Data.Entity;
using EventsApp.Entities;
namespace EventsApp.Contexts.DataContexts
{
public class RegistrantDb :DbContext
{
public RegistrantDb()
: base("EventsConnectionString")
{
}
public DbSet<Registrant> Registrants { get; set; }
}
}
My file structure
And the command to update the database:
Update-Database -ConfigurationTypeName EventsApp.Contexts.DataContexts.RegistrantMigrations.Configuration -verbose
Error message
Error Number:-1,State:0,Class:20
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)
I believe my settings are wrong so it can't find Events.mdf. Does anyone know what I need to change?
You should always check if you use the correct project to apply updates to (either select the correct project from the "Default Project" dropdown at the top of the package manager console or you can add -ProjectName to your Update-Database command:
Update-Database -ProjectName EventsApp.Contexts -YourOtherOptions

EntityFramework and update-database stopped working

I had everything working nicely using EF and code first until I changed the configuration class to add some extra seed data and then tried
Update-Database
Now I get the below error each time.
Specify the '-Verbose' flag to view the SQL statements being applied to the target database. System.Data.SqlClient.SqlException
(0x80131904): A network-related or instance-specific error occurred
while establishing a connection to SQL Server. The server was not
found or was not accessible. Verify that the instance name is correct
and that SQL Server is configured to allow remote connections.
(provider: SQL Network Interfaces, error: 26 - Error Locating
Server/Instance Specified)
I can connect and view the database in the sql object explorer and even view the tables and data. I'm at a loss and about to give up code first and goto db first or maybe even stored procedures soon.
My connection string looks like this
<add name="MyDbContext" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\MyDbContext.mdf;Initial Catalog=MyDb;Integrated Security=True" providerName="System.Data.SqlClient" />
Damn, I set the project as the startup project and then ran the command and it looks like its all worked now! Grrrr. I assumed selecting the project from Default project: selection dropdown did this but obviously not.
You can specify connection string via ConnectionString parameter:
Update-Database -ConnectionString "data source=server_name;initial catalog=db_name;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework" -ConnectionProviderName "System.Data.SqlClient" -Verbose
Also you need to use this parameter with the same value for Add-Migration command:
Add-Migration Version_Name -ConnectionString "data source=server_name;initial catalog=db_name;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework" -ConnectionProviderName "System.Data.SqlClient" -Verbose