Kategorie: C#

Sniplets und Merkzettel

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

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

    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.


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

  • Sortieren einer Liste

    ListName = ListName.OrderBy(q => q).ToList();


  • Merkzettel

    Variablen direkt in Strings ausgeben, ohne „concat“

    Console.WriteLine($"Hello {name}");


Wir benutzen Cookies um die Nutzerfreundlichkeit der Webseite zu verbessen. Durch Deinen Besuch stimmst Du dem zu.