The main objectives of this article are:
- Create .NET7 API Project
- Install Entity Framework Core Package
- Setup Database Context
Create A .NET7 Web API Project:
Let's create a sample .Net 7 API application to accomplish our demo.
(Step 1)
(Step 2)
(Step 3)
(Step 4)
Install Entity Framework Core NuGet Package:
Let's install the entity framework core package.
Let's install the entity framework core SQL NuGet package.
SQL Connection String:
Let's prepare the SQL connection string.
Sample SQL Connection String:
Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=MyWorldDB;Integrated Security=True;Connect Timeout=30
- Data Source - SQL server name.
- Initial Catalog - Database name
- Integrated Security -windows authentication.
- Connect Time -connection time period.
appsettings.Development.json:
"ConnectionStrings": { "MyWorldDbConnection": "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=MyWorldDB;Integrated Security=True;Connect Timeout=30" }
Configure Database Context:
Now add a new class that represents our 'SuperHeroes' table. So let's creat a new folder like 'Data/Entities' and then add our new class like 'SuperHeroes.cs'.
Data/Entities/SuperHeroes.cs:
namespace Dot7.Api.Data.Entities { public class SuperHeroes { public int Id { get; set; } public string? Name { get; set; } public string? Franchise { get; set; } public string? Gender { get; set; } public string? ImageURL { get; set; } } }To manage or control all the table classes in c# we have to create the DatabaseContext class. So let's create our context class like 'MyWorldDbContext.cs' in the 'Data' folder.
Data/MyWorldDbContext.cs:
using Microsoft.EntityFrameworkCore; namespace Dot7.Api.Data.Entities { public class MyWorldDbContext:DbContext { public MyWorldDbContext(DbContextOptions<MyWorldDbContext> options):base(options) { } public DbSet<SuperHeroes> SuperHeroes { get; set; } } }
- (Line: 4)The 'Microsoft.EntityFramework.DbContext' needs to be inherited by our 'MyWorldDbContext' to act as Database Context class.
- (Line: 6) The 'Microsoft.EntityFrameworkCore.DbContextOptions' is an instance of options that we are going to register in 'Program.cs' like 'ConnectionString', 'DatabaseProvider', etc.
- (Line: 9) All our table classes must be registered inside of our database context class with 'DbSet<T>' so that the entity framework can communicate with the table of the database.
Program.cs:
using Dot7.Api.Data.Entities; using Microsoft.EntityFrameworkCore; builder.Services.AddDbContext<MyWorldDbContext>(options => { options.UseSqlServer(builder.Configuration.GetConnectionString("MyWorldDbConnection")); });In the next article, we will implement the Read Operation.
Support Me!
Buy Me A Coffee
PayPal Me
Video Session:
Wrapping Up:
Hopefully, I think this article delivered some useful information on the.NET7 Blazor WebAssembly CRUD sample. using I love to have your feedback, suggestions, and better techniques in the comment section below.
Refer:
Part-8 | Blazor WebAssembly | .Net7 API | MudBlazor | CRUD - Implement Search
Part-9 | Blazor WebAssembly | .Net7 API | MudBlazor | CRUD - Implement Pagination
Part-9 | Blazor WebAssembly | .Net7 API | MudBlazor | CRUD - Implement Pagination
Comments
Post a Comment