From dd31874f717a371b207d9e27e178e9f005795c52 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Tue, 28 Mar 2023 12:34:45 +0300 Subject: [tutorials/ctmide] Updated code blocks to be proper markdown ones --- .../connect-to-mysql-in-dotnet-ef/index.md | 31 ++++++++++++---------- 1 file changed, 17 insertions(+), 14 deletions(-) (limited to 'src/tutorials') diff --git a/src/tutorials/connect-to-mysql-in-dotnet-ef/index.md b/src/tutorials/connect-to-mysql-in-dotnet-ef/index.md index d6bde42..c5f0e9d 100644 --- a/src/tutorials/connect-to-mysql-in-dotnet-ef/index.md +++ b/src/tutorials/connect-to-mysql-in-dotnet-ef/index.md @@ -39,8 +39,9 @@ The method requires at least two arguments: connection string and `MySql` server For the server version argument, create a new object of type `MySqlServerVersion` and give it's constructor a new object of type `Version`, to which you give the version numbers. The result should look something like this: -
.UseMySql(CONNECTION_STRING, new MySqlServerVersion(new System.Version(8, 0, 21)))
-
+```csharp +.UseMySql(CONNECTION_STRING, new MySqlServerVersion(new System.Version(8, 0, 21))) +``` Where `CONNECTION_STRING` is a (constant) variable that contains your connection string, and 8, 0, 21 are the version numbers. @@ -50,23 +51,25 @@ For `ASP.NET` applications, the connection must be put inside the `ConfigureServ The result should look something along the lines of: -
// This method gets called by the runtime. Use this method to add services to the container.
-public void ConfigureServices(IServiceCollection services)
-{   
-    ...
-    services.AddDbContext<AppDbContext>(opt =>
+```csharp
+// This method gets called by the runtime. Use this method to add services to the container.
+public void ConfigureServices(IServiceCollection services)
+{
+    ...
+    services.AddDbContext(opt =>
     {
-        opt.UseMySql(...);
+        opt.UseMySql(...);
     });
-}
-

+} +``` ### DBContext child class Another way to connect is via a `DBContext` child class (so, inside your own DB context). Just, override the `OnConfiguring` method and call the `.UseMySql` method from the `optionsBuilder` parameter. -
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
+```csharp
+protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
 {
-    optionsBuilder.UseMySql(...);
-}
-

+ optionsBuilder.UseMySql(...); +} +``` -- cgit v1.3.1-23-gd82d