Reputation: 37
I am working with blazor and I am applying code first approach
but issue is my database is not created
fist I create a project and then select a visual studio 2019 -> blazor app -> blazor server app
then
I add connetionstring in appsetting.json
{
"ConnectionStrings": {
"DefaultConnection": "Data Source=servername;Initial Catalog=databasename;User ID=userid;Password=password"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*"
}
then
above connectionstring name initialize in startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages();
services.AddServerSideBlazor();
services.AddDbContext<MyDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
}
and then I create a Emp class
Emp.cs
namespace BlazorServerApp.Pages
{
public class Emp:ComponentBase
{
public int empid { get; set; }
public string empname { get; set; }
public string empcountry { get; set; }
}
}
and then I create a mydbcontext class
MyDbContext.cs
namespace BlazorServerApp.Pages
{
public class MyDbContext:DbContext
{
public MyDbContext(DbContextOptions<MyDbContext> options)
: base(options)
{ }
public DbSet<Emp> emps { get; set; }
}
}
and then I crate a razor component(EmployeeList) in pags folder
EmployeeList.razor
@page "/"
@inherits Emp
<h3>EmployeeList</h3>
<div>
<div>
<h3>@empname @empcountry</h3>
</div>
</div>
and then run project but database is not created
issue is database not created?
help
Upvotes: 0
Views: 1723
Reputation: 961
You can first install the following package and then run your desired database by adding migration. Follow these steps:
first of all, install the following packages :
Microsoft.EntityFrameworkCore.Tools
Microsoft.EntityFrameworkCore.SqlServer
two add in Startup.cs :
using Microsoft.EntityFrameworkCore;
services.AddDbContext<Dbcontext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DatabaseConnection")));
Then add in the Package Manager Console :
Add-Migration
Update-Datatbase
Upvotes: 1
Reputation: 531
dotnet tool install --global dotnet-ef
dotnet add package Microsoft.EntityFrameworkCore.Design
dotnet ef migrations add InitialCreate
dotnet ef database update
Please refer to the official documentation here: https://learn.microsoft.com/en-us/ef/core/get-started/?tabs=netcore-cli#create-the-database
Upvotes: 1
Reputation: 37
just add this in startup.cs in configure method
var context = serviceScope.ServiceProvider.GetRequiredService<MyDbContext>();
context.Database.EnsureCreated();
Startup.cs
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
using (var serviceScope = app.ApplicationServices.GetService<IServiceScopeFactory>().CreateScope())
{
var context = serviceScope.ServiceProvider.GetRequiredService<MyDbContext>();
context.Database.EnsureCreated();
}
MyDbContext.cs
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
//using System.Data.Entity;
//using System.Data.Entity;
//using System.Data.Entity;
//using System.Data.Entity;
using System.Linq;
using System.Threading.Tasks;
namespace BlazorServerApp.Pages
{
public class MyDbContext: DbContext
{
public MyDbContext(Microsoft.EntityFrameworkCore.DbContextOptions<MyDbContext> options)
: base(options)
{
//System.Data.Entity.Database.SetInitializer<MyDbContext>(new CreateDatabaseIfNotExists<MyDbContext>());
}
public DbSet<Emp> emps { get; set; }
}
}
Just add this functionality in startup.cs page
Upvotes: 1
Reputation: 552
You need to nun the cammand "add-migration [MigrationName]" in the package manager(choose your project). Once the add migration command executes successfully, it creates a folder name as ‘Migration’ in the project and creates the class with the same name [MigrationName] as we have provided while executing add migration command with some name.
We have only created the migration script which is responsible for creating the database and its table. But we've not created the actual database and tables. So, let's execute the migration script and generate the database and tables. Therefore, executing the migration scripts we have to execute ‘update-database [MigrationName]’ command.
Upvotes: 1