This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
sqlite_dotnet_ef_vscode [2021/09/01 09:46] sausage created |
sqlite_dotnet_ef_vscode [2021/09/22 05:39] (current) sausage |
||
---|---|---|---|
Line 3: | Line 3: | ||
{{:sqlite:sqlite-ef-vscode.png?direct&400 |}} | {{:sqlite:sqlite-ef-vscode.png?direct&400 |}} | ||
- | One of the nice things about SQLite databases is that they are so much easier than other databases to spin up and work with. That's opposed to something like SQL Server and it's heavy overhead with a SQL Server installation. | + | One of the nice things about SQLite databases is that they are so quick to spin up and work with. That's opposed to the overhead of something like SQL Server and it's installation. |
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 35: | Line 39: | ||
===== Packages ===== | ===== Packages ===== | ||
- | To use Entity Framework and an API to access SQLite, we need the ''Microsoft.EntityFrameworkCore.Sqlite.'' package. | + | To use Entity Framework and an API to access SQLite, we need the ''Microsoft.EntityFrameworkCore.Sqlite'' package. |
This can be installed (in my case) for .NET 5 with: | This can be installed (in my case) for .NET 5 with: | ||
- | Install-Package Microsoft.EntityFrameworkCore.Sqlite -Version 5.0.9 | + | dotnet add package Microsoft.EntityFrameworkCore.Sqlite --version 5.0.9 |
It will depend on what version of .NET Core you are using. If you are not having luck installing the package, head over to ''nuget.org'': | It will depend on what version of .NET Core you are using. If you are not having luck installing the package, head over to ''nuget.org'': | ||
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> | ||