Showing posts with label EF CORE 6. Show all posts
Showing posts with label EF CORE 6. Show all posts

Tuesday, November 8, 2022

How to create database context class and adding tables to it? ASP.NET Core 6

CODE:

namespace projectnamespace;
public class Northwind : DbContext { 
var DataProvider="SQLite";
 
 protected override void OnConfiguring( DbContextOptionsBuilder     optionsBuilder) { 
 if (DatabaseProvider == "SQLite") { 
     string path = Path.Combine( Environment.CurrentDirectory, "Northwind.db"); 
 WriteLine($"Using {path} database file."); 

 optionsBuilder.UseSqlite($"Filename={path}"); 
 } else { 
     string connection = "Data Source=.;" + "Initial Catalog=Northwind;" + "Integrated Security=true;" + "MultipleActiveResultSets=true;";

     optionsBuilder.UseSqlServer(connection);
 } 
  public DbSet? Table1{ get; set; } 
  public DbSet? Table2{ get; set; }
}

Saturday, November 5, 2022

ASP.NET6 command to scafold modules using an existing database

NOTE : Make sure Microsoft.EntityFrameworkCore.Design package should be added to your project.

dotnet ef dbcontext scaffold "Filename=dbname.db" Microsoft. EntityFrameworkCore.Sqlite --table tablename --table tablename --outputdir Foldername --namespace Namespace --data-annotations --context Dbcontext

Note the following:


• The command action: dbcontext scaffold
• The connection string: "Filename=Northwind.db" 
• The database provider: Microsoft.EntityFrameworkCore.Sqlite 
• The tables to generate models for: --table Categories --table Products 
• The output folder: --output-dir AutoGenModels 
• The namespace: --namespace WorkingWithEFCore.AutoGen 
• To use data annotations as well as the Fluent API: --data-annotations 
• To rename the context from [database_name]Context: --context Northwind

asp.net core connection string for all databases in detail.

SQLITE

To make an SQLite database connection string, we just need to know the databse filename, set using the paramenter Filename.

Steps :


SQL Server

To connect to an SQL Server database, we need to know all these pieces of informations given.

  • Server name
  • Database name
  • Security informations such as username & password or if we pass the currently logged-on user's credentials automatically.
    We specify this information in a connection string

Steps :

methods available in Dapper

  Dapper is a micro ORM library for .NET and .NET Core applications that allows you to execute SQL queries and map the results to objects. D...