Basics

Use the commandline

# To activate the developer commandline
"C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvars64.bat"
# it generates ./a.exe
csc ./a.cs
# it generates b.dll
csc /target:library b.cs

# it generates a.exe
csc /r:b.dll a.cs
dumbpin -exports sherpa-onnx-c-api.dll

If the name in the C++ function is Hello, we can use either:

[DllImport("dllname")]
void Hello();

or

[DllImport("dllname", EntryPoint="Hello")]
void HelloANewName();

Visual Studio for Mac

After installation, start it and click Visual Stuido -> About Visual studio -> Show details, it will show the locations:

  • Runtime: /usr/local/share/dotnet/dotnet

  • SDK: /usr/local/share/dotnet/sdk/7.0.203/Sdks

$ /usr/local/share/dotnet/dotnet

Usage: dotnet [options]
Usage: dotnet [path-to-application]

Options:
  -h|--help         Display help.
  --info            Display .NET information.
  --list-sdks       Display the installed SDKs.
  --list-runtimes   Display the installed runtimes.

path-to-application:
  The path to an application .dll file to execute.

global usings

Requires C# >= 10.0

Create a file, e.g., GlobalUsings.cs:

nullable types

Product? p = products[0];
string val;
if (p != null) {
  val = p.Name;
} else {
  val = "No value";
}
return View(new string[] { val });

string? val = products[0]?.Name;
if (val != null) {
  return View(new string[] { val });
}
return View(new string[] { "No Value" });

return View(new string[] { products[0]?.Name ?? "No Value" });

return View(new string[] { products[0]!.Name });

Dictionary

Dictionary<string, Product> products = new Dictionary<string, Product> {
  { "Kayak", new Product { Name = "Kayak", Price = 275M } },
  { "Lifejacket",  new Product{ Name = "Lifejacket", Price = 48.95M } }
};

Dictionary<string, Product> products = new Dictionary<string, Product> {
  ["Kayak"] = new Product { Name = "Kayak", Price = 275M },
  ["Lifejacket"] = new Product { Name = "Lifejacket", Price = 48.95M }
};

Dictionary<string, Product> products = new() {
  ["Kayak"] = new Product { Name = "Kayak", Price = 275M },
  ["Lifejacket"] = new Product { Name = "Lifejacket", Price = 48.95M }
};

foreach

public class ShoppingCart : IEnumerable<Product?> {
  public IEnumerable<Product?>? Products { get; set; }

  public IEnumerator<Product?> GetEnumerator() => Products?.GetEnumerator() ?? Enumerable.Empty<Product?>().GetEnumerator();

  IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}

my first page

@model MyMessage
@{
    Layout = null;
}
<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width"/>
    <title>Hello my page</title>
    <style>
    </style>
</head>
<body>
    <h1> please fill the form</h1>
    <form asp-action="Index" method="post">
        <div>
            <label asp-for="Id">Id</label>
            <input asp-for="Id"/>
        </div>
        <div>
            <label asp-for="Name">Name</label>
            <input asp-for="Name"/>
        </div>
        <button type="submit">sumbit</button>
        <a asp-action="Index">clear</a>
    </form>
</body>
</html>

EF

  • Microsoft.EntityFrameworkCore.SQLite

  • Microsoft.VisualStudio.Web.CodeGeneration.Design

  • Microsoft.EntityFrameworkCore.SqlServer

  • Microsoft.EntityFrameworkCore.Design

  • Microsoft.EntityFrameworkCore.Tools

dotnet tool uninstall --global dotnet-aspnet-codegenerator
dotnet tool install --global dotnet-aspnet-codegenerator
dotnet tool uninstall --global dotnet-ef
dotnet tool install --global dotnet-ef

export PATH=$HOME/.dotnet/tools:$PATH
dotnet aspnet-codegenerator controller -name MoviesController -m Movie -dc MvcMovie.Data.MvcMovieContext --relativeFolderPath Controllers --useDefaultLayout --referenceScriptLibraries -sqlite
Building project ...
Finding the generator 'controller'...
Running the generator 'controller'...
--useSqlite|-sqlite option is obsolete now. Use --databaseProvider|-dbProvider instead in the future.

Minimal hosting scenario!
Generating a new DbContext class 'MvcMovie.Data.MvcMovieContext'
Attempting to compile the application in memory with the added DbContext.
Attempting to figure out the EntityFramework metadata for the model and DbContext: 'Movie'

Using database provider 'Microsoft.EntityFrameworkCore.Sqlite'!

Added DbContext : '/Data/MvcMovieContext.cs'
Added Controller : '/Controllers/MoviesController.cs'.
Added View : /Views/Movies/Create.cshtml
Added View : /Views/Movies/Edit.cshtml
Added View : /Views/Movies/Details.cshtml
Added View : /Views/Movies/Delete.cshtml
Added View : /Views/Movies/Index.cshtml
diff --git a/MvcMovie/Program.cs b/MvcMovie/Program.cs
index 9fbb57d..b96f671 100644
--- a/MvcMovie/Program.cs
+++ b/MvcMovie/Program.cs
@@ -1,4 +1,9 @@
-var builder = WebApplication.CreateBuilder(args);
+using Microsoft.EntityFrameworkCore;
+using Microsoft.Extensions.DependencyInjection;
+using MvcMovie.Data;
+var builder = WebApplication.CreateBuilder(args);
+builder.Services.AddDbContext<MvcMovieContext>(options =>
+    options.UseSqlite(builder.Configuration.GetConnectionString("MvcMovieContext") ?? throw new InvalidOperationException("Connection string 'MvcMovieContext' not found.")));

 // Add services to the container.
 builder.Services.AddControllersWithViews();
diff --git a/MvcMovie/appsettings.json b/MvcMovie/appsettings.json
index af0538f..d8b9276 100644
--- a/MvcMovie/appsettings.json
+++ b/MvcMovie/appsettings.json
@@ -1,10 +1,12 @@
-{
+{
   "Logging": {
     "LogLevel": {
       "Default": "Information",
       "Microsoft.AspNetCore": "Warning"
     }
   },
-  "AllowedHosts": "*"
-}
-
+  "AllowedHosts": "*",
+  "ConnectionStrings": {
+    "MvcMovieContext": "Data Source=MvcMovieContext-95c663f1-d863-4557-a405-0d9cf818bb16.db"
+  }
+}
dotnet ef migrations add InitialCreate