Um auf einem Ubuntu-Server das Remote Dedugging zu installieren (Achtung: sollte man nicht auf einen Produktionsserver tun!):
curl -sSL https://aka.ms/getvsdbgsh | bash /dev/stdin -v latest -l ~/.vs-debugger
Sniplets und Merkzettel
Um auf einem Ubuntu-Server das Remote Dedugging zu installieren (Achtung: sollte man nicht auf einen Produktionsserver tun!):
curl -sSL https://aka.ms/getvsdbgsh | bash /dev/stdin -v latest -l ~/.vs-debugger
aspnet-codegenerator installieren:
dotnet tool install -g dotnet-aspnet-codegenerator --version 6.0.0
dotnet aspnet-codegenerator identity -dc ApplicationDbContext
oder wenn vorhanden:
dotnet aspnet-codegenerator identity -dc ApplicationDbContext --force
dotnet add package Microsoft.VisualStudio.Web.CodeGeneration.Design
dotnet add package Microsoft.VisualStudio.Web.CodeGeneration.Design --version 6.0.0
dotnet aspnet-codegenerator identity -dc MyBlazorApp.Data.ApplicationDbContext --files "Account.Register;Account.Login;Account.Logout"
Quelle:
https://learn.microsoft.com/de-de/aspnet/core/fundamentals/tools/dotnet-aspnet-codegenerator?view=aspnetcore-8.0
For a Blazor Server App, you’d typically manage the EF Core functionality in the Server project. So, make sure you are working in the Server project directory or set the Server project as the default project in the Package Manager Console (PMC) in Visual Studio.
Using the NuGet Package Manager or the PMC, install the following packages:
Microsoft.EntityFrameworkCore
Microsoft.EntityFrameworkCore.SqlServer
Microsoft.EntityFrameworkCore.Tools
(for migration tools)Create classes that will define the structure of your database tables. Place these in the Shared or Server project, depending on your design decisions.
Example:
public class Product
{
public int ProductId { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
}
In the Server project, create a class that derives from DbContext
:
public class AppDbContext : DbContext
{
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options) { }
public DbSet<Product> Products { get; set; }
}
In the appsettings.json
file in the Server project, add your connection string:
{ "ConnectionStrings": { "DefaultConnection": "
Server=Servername;Database=DatabaseName;user=UserId;password=secret;MultipleActiveResultSets=true;TrustServerCertificate=True;MultiSubnetFailover=True" } }
Replace YourConnectionStringHere
with your MS SQL Server 2022 connection string.
In the Startup.cs
(or Program.cs
for newer versions) of your Server project, register the DbContext
in the ConfigureServices
method:
builder.Services.AddDbContext<AppDbContext>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));
(Do not forget to import EF Core)
In the PMC or terminal, navigate to the Server project directory and run:
Add-Migration InitialCreate
This will scaffold a migration to create the initial set of tables for your model.
Apply the migration to create the database schema:
Update-Database
Now you can inject and use your DbContext
in your pages, components, or services:
csharpCopy code@inject AppDbContext _context
// ...
var products = await _context.Products.ToListAsync();
That’s the basic setup! As your app grows, you might add more entities, relationships, migrations, and other advanced EF Core features. Ensure you understand concepts like the lifecycle of a DbContext and how it tracks changes to entities to avoid issues when manipulating data.
Installation nuget Packages in V 7.0.0
Microsoft.Extensions.Configuration
Microsoft.Extensions.Configuration.Json
Neue Datei appsettings.json im root-Verzeichnis erstellen
In z.B. Program.cs:
public static IConfiguration _conf;
var configuration = new ConfigurationBuilder()
.AddJsonFile($"appsettings.json");
_conf = configuration.Build();
Connection String auslesen:
string connectionString = _conf.GetConnectionString("DefaultConnection")!;
Setting auslesen:
string s = _conf.GetSection("section").Value!;
Verschachtelte Settings auslesen:
"MainSection": {
"SubSection": {
"SubSubSection": "value"
}
string s2 = _conf.GetSection("section:SubSection:SubSubSection").Value!;