Skip to content

sulmar/Altkom.DotNetCore.201810

Repository files navigation

Altkom.DotNetCore.201810

Polecenia z linii polecen

  • 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>

Entity Framework

  • 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)");
			  

Migracje

  1. Instalacja narzędzi do Visual Studio
PM> Install-Package Microsoft.EntityFrameworkCore.Tools
  1. 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);
        }
    }

Utworzenie bazy danych

PowerShell

Add-Migration InitialCreate

Konsola

dotnet ef migrations add InitialCreate

Zastosowanie migracji do utworzenie bazy danych

PowerShell

Update-Database

Konsola

dotnet ef database update

Dodanie migracji

PowerShell

Add-Migration AddCustomerCity

Konsola

dotnet ef migrations add AddCustomerCity
  1. Zastosuj migrację
Update-Database

Usuwanie migracji

PowerShell

Remove-Migration

Konsola

dotnet ef migrations remove

Powracanie do migracji

PowerShell

Update-Database LastGoodMigration

Konsola

dotnet ef database update LastGoodMigration

Generowanie skryptu SQL

PowerShell

Script-Migration

Konsola

dotnet ef migrations script

Narzędzia

  • Ngrok - umożliwia wystawienie własnej usługi pod adresem publicznym https://ngrok.com/

About

No description or website provided.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages