I am taking a tutorial on the entity framework in hopes of tying together my database/ETL/SQL skills from my job and my C# dev skills from Unity.
Hence, this is my first project for the .NET framework outside of the unity C# world.
Setup: I have a local DB created on my computer that I've successfully connected to via SQL ManagementStudio. I have two model classes, an Author and a Book. I have LibraryContext class that extends from DbContext, and a HomeController that extends from Controller. An additional line has been added to the ConfigureServices() method in the Startup.cs, other than that the Startup.cs class is unchanged. See below for that.
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<LibraryContext>(option => option.UseSqlServer(Configuration.GetConnectionString("LibraryDemo1"))); //this is the additional code
services.AddRazorPages();
}
Intended Behavior: Upon execution, the intention is to have the two model classes (Author and Book) be created as tables in the local Db. This is not happening.
Actual Behavior: The tables are not being created, and a web page pops up (see below image)
It doesn't appear like my code is being executed, because when I change the DB connection string, for example, to some nonsense, it doesn't even throw an error. So it's as if it's not even trying to reach out to the database.
I feel like there some larger thing that I'm not understanding here, perhaps about the project setup or configuration. For example, I don't know how the Index() method below would even get called, and there's no place in my tutorial where I see a call to it. Please see the below relevant code. I won't post the Author and Book classes, since those are pretty straightforward.
Any help would be really appreciated. Thank you!
//lots of Usings above here, not posting for the sake of space
namespace CodeFirstEntityFramework.Data
{
public class LibraryContext : DbContext
{
public LibraryContext(DbContextOptions<LibraryContext> options)
: base(options) //this calls the base constructor from the DbContext class
{
}
//the DbSet represents an entire table
public DbSet<Author> Author { get; set; }
public DbSet<Book> Book { get; set; }
}
}
namespace CodeFirstEntityFramework.Controllers
{
public class HomeController : Controller
{
private LibraryContext _context;
public HomeController(LibraryContext context)
{
_context = context;
}
public string Index()
{
_context.Database.EnsureCreated();
return "DB has been created";
}
}
}
CodePudding user response:
First you should add a migration of the database - if using powershell use dotnet ef migrations add MigrationName or Add-Migration MigrationName with package manager console.Then for powershell dotnet ef database update or for PM console Update-Database to commit any changes for the DB/Create the DB. You have that page because in your HomeController there is an IActionResult Index() method that returns View(). To get the text "DB has been created" you should find the Index.cshtml file in folder Views or Pages in your case and add a or something that will get the text. Note that the text should be passed with a ViewModel to the View. You can check this tutorial to get you started -> https://docs.microsoft.com/en-us/aspnet/core/data/ef-mvc/intro?view=aspnetcore-6.0 . You should check this https://docs.microsoft.com/en-us/aspnet/core/mvc/views/overview?view=aspnetcore-6.0 about the views and view models to get the idea about the whole .net model-view-controller.
CodePudding user response:
From your image it seems you created an ASP.NET Web App with razor pages instead of a MVC project.
For EnsureCreated to be executed, you need to put it in the Index.html.cs file, inside the OnGet() Method.
public class IndexModel : PageModel
{
private readonly ILogger<IndexModel> _logger;
private LibraryContext _context;
public IndexModel(ILogger<IndexModel> logger,LibraryContext context)
{
_logger = logger;
_context = context;
}
public void OnGet()
{
_context.Database.EnsureCreated();
}
}
If more errors arise then it may be a problem on the configuration too, so I would ask to see the Startup.cs