This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
sqlite_dotnet_ef_vscode [2021/09/22 05:20] sausage [Creating a .NET Core application with Entity Framework and SQLite in VSCode] |
sqlite_dotnet_ef_vscode [2021/09/22 05:39] (current) sausage |
||
---|---|---|---|
Line 6: | Line 6: | ||
Also that SQLite databases can sit comfortably with the application itself for deployments of simple web architectures. There is something simply refreshing about that. | Also that SQLite databases can sit comfortably with the application itself for deployments of simple web architectures. There is something simply refreshing about that. | ||
+ | |||
+ | And EF is getting better and easier all the time. | ||
+ | |||
+ | Being able to do all this with .NET Core in a light and flexible IDE like VSCode makes the experience much more pleasant. | ||
===== Setting up a basic MVC project ===== | ===== Setting up a basic MVC project ===== | ||
Line 22: | Line 26: | ||
Create the project by typing the following into the terminal: | Create the project by typing the following into the terminal: | ||
- | dotnet core mvc | + | dotnet new mvc |
Your project has been created, but cannot be debugged yet without the debug extension being loaded. | Your project has been created, but cannot be debugged yet without the debug extension being loaded. | ||
Line 96: | Line 100: | ||
namespace SQLiteLab | namespace SQLiteLab | ||
{ | { | ||
- | public class SqliteDbContext : DbContext { | + | public class SqliteDbContext : DbContext |
+ | { | ||
//entities representing tables in the database | //entities representing tables in the database | ||
Line 156: | Line 161: | ||
===== Producing the database ===== | ===== Producing the database ===== | ||
- | To make the database, we need to add a "migration" and then perform a database update that will use that migration. So what's a migration? It's either the information to make the whole database je first time, or incremental changes to a database schema. It allows you to change your database schema over time. | + | To make the database, we need to add a "migration" and then perform a database update that will use that migration. So what's a migration? It's either the information to make the whole database the first time, or incremental changes to a database schema. It allows you to change your database schema over time. |
To make a migration, you need the ''Microsoft.EntityFrameworkCore.Design'' package. Again, use the search at nuget.org to get the correct .NET CLI command to use. In my case, for .NET 5 it was: | To make a migration, you need the ''Microsoft.EntityFrameworkCore.Design'' package. Again, use the search at nuget.org to get the correct .NET CLI command to use. In my case, for .NET 5 it was: | ||
Line 222: | Line 227: | ||
<code c> | <code c> | ||
- | private void CreateGuidRecords() | + | private void CreateGuidRecords() |
- | { | + | { |
- | _context.GuidInfos.Add(new GuidInfo { | + | _context.GuidInfos.Add(new GuidInfo { |
- | Guid = new Guid("D06FACE5-CA6E-D1CE-FAD5-D0661E555555"), | + | Guid = new Guid("D06FACE5-CA6E-D1CE-FAD5-D0661E555555"), |
- | Feature = "Has dogs, cages, dices and dog faces." | + | Feature = "Has dogs, cages, dices and dog faces." |
- | }); | + | }); |
- | _context.GuidInfos.Add(new GuidInfo { | + | _context.GuidInfos.Add(new GuidInfo { |
- | Guid = new Guid("01101101-0110-0001-0111-010001100101"), | + | Guid = new Guid("01101101-0110-0001-0111-010001100101"), |
- | Feature = "MATE in binary. Good to have your mates." | + | Feature = "MATE in binary. Good to have your mates." |
- | }); | + | }); |
- | + | ||
- | _context.SaveChanges(); | + | _context.SaveChanges(); |
- | + | } | |
- | } | + | |
</code> | </code> | ||
Line 242: | Line 246: | ||
<code c> | <code c> | ||
- | public IActionResult Index() | + | public IActionResult Index() |
- | { | + | { |
- | CreateGuidRecords(); | + | CreateGuidRecords(); |
- | return View(); | + | return View(); |
- | } | + | } |
</code> | </code> | ||