using Microsoft.EntityFrameworkCore;
using WebApp.Models;
namespace WebApp.Data
{
public class WebAppContext : DbContext
{
public WebAppContext(DbContextOptions<WebAppContext> options) : base(options)
{}
public DbSet<Message> Message { get; set; }
}
}
Message.cs
using System;
namespace WebApp.Models
{
public class Message
{
public int Id { get; set; }
public string Title { get; set; }
public string Content { get; set; }
}
}
MessageController
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using WebApp.Data;
using WebApp.Models;
using System;
using System.Linq;
namespace WebApp.Controllers
{
public class MessageController : Controller
{
private readonly WebAppContext _context;
public MessageController(WebAppContext context)
{
_context = context;
}
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult PublishMessage()
{
var db = new WebAppContext(new DbContextOptions<WebAppContext>{});
var new_message = new Message{
Title = Request.Form["Title"],
Content = Request.Form["Content"]
};
db.Add(new_message);
db.SaveChanges();
return RedirectToAction("Index", "Home");
}
}
}
> dotnet ef migrations add InitialCreate // Create the migration table
> dotnet ef database update // Create/Update the DB according to the migration schema
And running my app then Clicking on the publish button, I get :
InvalidOperationException: No database provider has been configured for this DbContext.
Why ? I followed the offical tutorial and don't understand where I'm missing something.
public class BloggingContext : DbContext
{
public DbSet<Blog> Blogs { get; set; }
public DbSet<Post> Posts { get; set; }
public BloggingContext(DbContextOptions<BloggingContext> options) : base(options)
{
}
}
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages();
services.AddDbContext<BloggingContext>(options =>
options.UseSqlite(Configuration.GetConnectionString("BloggingContext")));
}
However, your controller does not follow standards. You used constructor injection but do not use the Bdcontext and try to create another context in the Action. The code will fail due to a missing connections string. Also the Action should have an input
model.
private readonly BloggingContext _bloggingContext;
public HomeController(BloggingContext bloggingContext)
{
_bloggingContext = bloggingContext;
}
// GET: HomeController
public ActionResult Index()
{
List<Blog> model = _bloggingContext.Blogs.ToList();
return View(model);
}
Thank you for your answer, you are right, I didn't follow the D.I I created, it now works !
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using WebApp.Data;
using WebApp.Models;
using System;
using System.Linq;
namespace WebApp.Controllers
{
public class MessageController : Controller
{
private readonly WebAppContext _context;
public MessageController(WebAppContext context)
{
_context = context;
}
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult PublishMessage()
{
var new_message = new Message{
Title = Request.Form["Title"],
Content = Request.Form["Content"]
};
_context.Add(new_message); // Using the _context object
_context.SaveChanges(); // Using the _context object
return RedirectToAction("Index", "Home");
}
}
}
To put it in a nutshell, with ASP.NET Core MVC one should use DB context this way for usage in a Controller :
public class MessageController : Controller
{
private readonly WebAppContext _context;
public MessageController(WebAppContext context)
{
_context = context;
}
Member
3 Points
43 Posts
[MVC] DbProvider issue with Sqlite 3.1
Dec 19, 2020 10:20 AM|valenciano8|LINK
Hi everyone,
I'm following the ASP.NET Core MVC tutorials to work with a database : https://docs.microsoft.com/en-us/aspnet/core/tutorials/first-mvc-app/working-with-sql?view=aspnetcore-3.1&tabs=visual-studio-code
So I did the following :
Startup.cs
appsettings.json
WebAppContext
Message.cs
MessageController
Index.cshtml
Yet after having configured everything :
And running my app then Clicking on the publish button, I get :
Why ? I followed the offical tutorial and don't understand where I'm missing something.
Thank you in advance for your help
All-Star
52101 Points
23232 Posts
Re: [MVC] DbProvider issue with Sqlite 3.1
Dec 19, 2020 01:01 PM|mgebhard|LINK
I cannot reproduce this issue.
However, your controller does not follow standards. You used constructor injection but do not use the Bdcontext and try to create another context in the Action. The code will fail due to a missing connections string. Also the Action should have an input model.
Member
3 Points
43 Posts
Re: [MVC] DbProvider issue with Sqlite 3.1
Dec 19, 2020 02:32 PM|valenciano8|LINK
Hi @mgebhard,
Thank you for your answer, you are right, I didn't follow the D.I I created, it now works !
To put it in a nutshell, with ASP.NET Core MVC one should use DB context this way for usage in a Controller :
All-Star
52101 Points
23232 Posts
Re: [MVC] DbProvider issue with Sqlite 3.1
Dec 19, 2020 02:48 PM|mgebhard|LINK
You should also pass a model to the Action as illustrated in every beginning level MVC tutorial.