- Utworzenie nowej aplikacji konsolowej
dotnet new console
- Utworzenie nowej aplikacji webapi
dotnet new webapi
- Utworzenie nowej aplikacji MVC
dotnet new mvc
- Uruchomienie aplikacji
dotnet helloworld.dll
- Uruchomienie testów jednostkowych
dotnet test
- Dodanie pakietu
dotnet add package <nazwa>
- Instalacja Entity Framework
Install-Package Microsoft.EntityFrameworkCore
- Pobranie connection string z pliku konfiguracyjnego appsettings.json
"ConnectionStrings": {
"MyConnection": "Data Source=(localdb)\\MSSQLLocalDb;Initial Catalog=MyDb;Integrated Security=true"
}
string connectionString = Configuration.GetConnectionString("MyConnection");
- Instalacja obsługi bazy danych SQL Server
Install-Package Microsoft.EntityFrameworkCore.SqlServer
Dzięki temu możemy użyć metodę UseSqlServer
services.AddDbContext<MyContext>(options =>
options.UseSqlServer(connectionString));
- Instalacja obsługi relacyjnej bazy danych, np. dodanie metodę HasColumnType
Install-Package Microsoft.EntityFrameworkCore.Relational
Przykład:
builder
.Property(p => p.UnitPrice)
.HasColumnType("decimal(10,2)");
- Instalacja narzędzi do Visual Studio
PM> Install-Package Microsoft.EntityFrameworkCore.Tools
- Utwórz klasę
public class MyContextFactory : IDesignTimeDbContextFactory<MyContext>
{
public MyContext CreateDbContext(string[] args)
{
IConfigurationRoot configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json")
.Build();
var optionsBuilder = new DbContextOptionsBuilder<MyContext>();
optionsBuilder.UseSqlServer(configuration.GetConnectionString("MyConnection"));
return new MyContext(optionsBuilder.Options);
}
}
PowerShell
Add-Migration InitialCreate
Konsola
dotnet ef migrations add InitialCreate
PowerShell
Update-Database
Konsola
dotnet ef database update
PowerShell
Add-Migration AddCustomerCity
Konsola
dotnet ef migrations add AddCustomerCity
- Zastosuj migrację
Update-Database
PowerShell
Remove-Migration
Konsola
dotnet ef migrations remove
PowerShell
Update-Database LastGoodMigration
Konsola
dotnet ef database update LastGoodMigration
PowerShell
Script-Migration
Konsola
dotnet ef migrations script
- Ngrok - umożliwia wystawienie własnej usługi pod adresem publicznym https://ngrok.com/