https://chatgpt.com/share/f3b0068a-a07f-49d5-a920-889ce7777391
Kategorie: Programmieren
-
.NET 6 & .NET 8 auf einem Ubuntu-Server installieren und ansprechen
To install .NET 6 and .NET 8 in parallel on Ubuntu Server and access these versions, follow these steps:
- Add Microsoft Package Signing Key to your list of trusted keys and add the package repository: First, update the package index and install the required dependencies:
sudo apt-get update; \ sudo apt-get install -y apt-transport-https && \ sudo apt-get update && \ sudo apt-get install -y dotnet-sdk-6.0 dotnet-sdk-8.0
- Install .NET SDKs: You can install both .NET 6 and .NET 8 SDKs using the following commands:
sudo apt-get install -y dotnet-sdk-6.0 sudo apt-get install -y dotnet-sdk-8.0
- Accessing the Specific .NET Versions: After installation, you can specify which version of the .NET SDK to use for a specific project by using a global.json file in the project directory. To specify the SDK version, create a
global.json
file with the following content: For .NET 6:
{ "sdk": { "version": "6.0.0" } }
For .NET 8:
{ "sdk": { "version": "8.0.0" } }
Alternatively, you can use the
dotnet --list-sdks
command to list all installed SDKs anddotnet --version
to see the default SDK version. Use thedotnet new globaljson --sdk-version <version>
command to generate aglobal.json
for a specific version.
-
Visual Studio Code Remote Development
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
-
Login-Seiten für MS-Identity
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 --forcedotnet add package Microsoft.VisualStudio.Web.CodeGeneration.Design
dotnet add package Microsoft.VisualStudio.Web.CodeGeneration.Design --version 6.0.0dotnet 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
-
EntityFramework.Core in eine .net Core Blazor WebApp integrieren
1. Install necessary packages:
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)
2. Define your Model:
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; } }
3. Create a DbContext:
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; } }
4. Configure the Connection String:
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.5. Register DbContext:
In the
Startup.cs
(orProgram.cs
for newer versions) of your Server project, register theDbContext
in theConfigureServices
method:builder.
Services.AddDbContext<AppDbContext>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));(Do not forget to import EF Core)
6. Create Initial Migration:
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.
7. Update the Database:
Apply the migration to create the database schema:
Update-Database
8. Use the DbContext in your app:
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.
-
How to host asp.net core 6 applications under Ubuntu
Schritt-für-Schritt-Anleitung zur Installation von ASP.NET Core 6 unter Ubuntu
1. Zuerst musst du sicherstellen, dass dein System auf dem neuesten Stand ist. Öffne ein Terminal und führe folgenden Befehl aus: `sudo apt-get update`.
2. Installiere die .NET Core SDK-Pakete, indem du folgenden Befehl ausführst: `sudo apt-get install dotnet-sdk-6.0`.
3. Um sicherzustellen, dass die Installation erfolgreich war, führe den folgenden Befehl aus: `dotnet –version`. Wenn die Version 6.0 angezeigt wird, ist die Installation erfolgreich.
4. Um ein neues ASP.NET Core-Projekt zu erstellen, führe den folgenden Befehl aus: `dotnet new webapp -o `. Dies erstellt ein neues ASP.NET Core-Projekt mit dem angegebenen Namen.
5. Um das Projekt zu starten, navigiere in das Projektverzeichnis und führe den folgenden Befehl aus: `dotnet run`.
6. Um das Projekt zu testen, öffne einen Webbrowser und gehe zu `http://localhost:5000`. Wenn alles richtig eingerichtet ist, solltest du die Standardseite sehen.
7. Fertig! Du hast ASP.NET Core 6 unter Ubuntu installiert.
-
appsettings.json erstellen
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!;
-
Alle Dateien in einem Verzeichnis auflisten
Dim files() As String = IO.Directory.GetFiles("c:\") For Each file As String In files ' file = Dateiname Next