Тёмный

.NET Core Generic Host w/ Entity Framework Migrations - FULL STACK WPF (.NET CORE) MVVM #22 

SingletonSean
Подписаться 23 тыс.
Просмотров 4,9 тыс.
50% 1

Learn how to add the .NET Core Generic Host to a WPF application. The Host is useful for managing application resources, such as dependency injection, logging, and configuration settings. It is also useful for running Entity Framework Core migrations without a design-time DB context factory.
In this series, I walk through the development of a WPF application from the database layer to the UI layer.
HOST EF MIGRATIONS DOCUMENTATION: docs.microsoft...
SOURCE CODE: github.com/Sin...
FULL PLAYLIST: • Full Stack WPF MVVM

Опубликовано:

 

28 сен 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 18   
@jcturpin8737
@jcturpin8737 3 года назад
Fantastic. I never knew I needed a host builder, now I feel that I cannot live without one. TYSM! I still kept in a couple of my development helpers for the startup command: protected override async void OnStartup(StartupEventArgs e) { _host.Start(); //Builds Database if not present and/or automatically migrates to latest version var factory = _host.Services.GetRequiredService(); var context = factory.CreateDbContext(); await context.Database.MigrateAsync().ConfigureAwait(true); //Ensures that a Development Login Account Exists IAuthenticationService authenticationService = new AuthenticationService(_host.Services.GetRequiredService(), _host.Services.GetRequiredService()); await authenticationService.EnsureDevAuthorAccountExists(); Window window = _host.Services.GetRequiredService(); window.Show(); base.OnStartup(e); }
@jirimoucka4389
@jirimoucka4389 2 года назад
Thanks for great video. I have got an error with new migration. I am using Rider and the error was "Unable to create an object of type 'SimpleTraderDbContext'. For the different patterns supported at design time,". I solved out to use in add migration and in database update adding command --startup-project /address of SimpleTrader.WPF/
@holyghost5763
@holyghost5763 Год назад
Thanks for the tip. Also you can set project as a startup in solution explorer.
@k1ntoho
@k1ntoho 4 года назад
YEAH, new tutorial! Thank you
@MsQlan
@MsQlan 3 года назад
Hi Sean, do you have a recommendation for allowing a user to save settings to appsettings.json? I've been looking around at different solutions to this as the IConfiguration/IOptions interfaces do not seem to support writing back to the json file. I've found solutions where the host configurator is extended with an IOptionsWritable interface and some others that write directly to the json from a seperate class and have the host automatically refresh as it detects changes to the file. But all feel rather complex to get around this issue. Maybe the simplest way is still writing your own settings class that serializes a model to XML/json and injecting it as a singleton?
@SingletonSean
@SingletonSean 3 года назад
Good question MsQlan! I received a similar question the other day, so I might take a closer look into saving settings into appsettings.json. However, I usually just setup a seperate class, serialize my settings to a JSON string, and then write the JSON string to a custom JSON file. Here's an example on lines 41-42 (ignore the other ugly stuff in this file hahaha) 😀: github.com/SingletonSean/design-patterns-csharp/blob/4fd30f951d53fe0677c0b880e36b56d0172bee7d/AbstractFactory/Services/ProductWriters/FileProductWriter.cs#L41
@alexn4217
@alexn4217 3 года назад
Well, I am stumped. My app.xml.cs is identical to Sean't source code in the repo branch for this video, my dbcontext and dbcontextfactory files are also the same, but I keep hitting this error on add-migration. I added the -v switch for more detail and this is what I am seeing: Microsoft.EntityFrameworkCore.Design.OperationException: Unable to create an object of type 'SimpleTraderDBContext'. For the different patterns supported at design time, see go.microsoft.com/fwlink/?linkid=851728 ---> System.InvalidOperationException: Unable to resolve service for type 'Microsoft.EntityFrameworkCore.DbContextOptions' while attempting to activate 'SimpleTrader.EntityFramework.SimpleTraderDBContext'. I clicked on the help link and maybe because I am dumber than most, it didn't make much sense. I am running my projects on .Net Core 3.1. Could that be the issue in the nutshell? Thanks!
@krzysztofwozniczak4747
@krzysztofwozniczak4747 3 года назад
I had the same problem, to fix it: 1. Change Startup Project to: "SimpleTrade.WPF". 2. In Package Manager Console set "Default project" to "SimpleTrader.EntityFramework" Shoud works!
@ragnarok7976
@ragnarok7976 2 месяца назад
​@@krzysztofwozniczak4747 You are my hero! I guess after seeing him have to change the startup project to entity core in the previous videos I just assumed that was always the way to do it. In hindsight that makes perfect sense as the thing the migration is looking for is not in that project anymore. Thank you so much for taking the time to make this comment!!!
@brandonberisford
@brandonberisford 3 года назад
Hey Sean, thanks for the videos once again. I Followed everything up to around 10 minutes and when trying to run I keep getting an exception saying "System.InvalidOperationException: 'Unable to resolve service for type 'System.String' while attempting to activate 'SimpleTrader.EntityFramework.SimpleTraderDbContextFactory'.'" Any idea why this might be happening? I've tried to trouble shoot be no luck so far.
@brandonberisford
@brandonberisford 3 года назад
This is my Create Host Builder method: public static IHostBuilder CreateHostBuilder(string[] args = null) { return Host.CreateDefaultBuilder(args) .ConfigureAppConfiguration(c => { c.AddJsonFile("appsettings.json"); }) .ConfigureServices((context, services) => { string connectionString = context.Configuration.GetConnectionString("default"); services.AddDbContext(o => o.UseSqlServer(connectionString)); services.AddSingleton( new SimpleTraderDbContextFactory(connectionString)); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddScoped(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(services => new HomeViewModel(MajorIndexListingViewModel .LoadMajorIndexViewModel( services.GetRequiredService()), services.GetRequiredService())); services.AddSingleton(services => { return () => services.GetRequiredService(); }); services.AddSingleton(services => { return () => services.GetRequiredService(); }); services.AddSingleton(services => { return () => services.GetRequiredService(); }); services.AddSingleton(services => { return () => new LoginViewModel( services.GetRequiredService(), new Renavigator( services.GetRequiredService(), services.GetRequiredService())); }); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddScoped(); services.AddScoped(s => new MainWindow(s.GetRequiredService())); }); }
@SingletonSean
@SingletonSean 3 года назад
Hey Brandon, this is because the DbContextFactory takes a string in the constructor, and of course "string" is not registered in the DI container. I forget which video I fix this in, but the issue should be resolved in source control. Let me know if you run into any issues!
@brandonberisford
@brandonberisford 3 года назад
@@SingletonSean Thanks for the response! Can you clarify what you mean by the issue being resolved in source control?
@SingletonSean
@SingletonSean 3 года назад
Hey Brandon, I was suggesting viewing the source code at github.com/SingletonSean/SimpleTrader. However, I see that you are registering the SimpleTraderDbContextFactory twice! The second registration (between IAccountService and IBuyStockService) should be removed.
@brandonberisford
@brandonberisford 3 года назад
@@SingletonSean That solved the problem!! Sometimes it's those small things you miss that others can see right away lol...thanks man!
@VinuP2023
@VinuP2023 4 года назад
Sean Thank you so much. How many more videos going to be there in this series? This is a very good tutorials series. :)
@SingletonSean
@SingletonSean 4 года назад
Many more videos Vinay! Maybe 30? Not sure exactly, as many as it takes to have a solid application that goes through the entire lifecycle of an application, haha.
@VinuP2023
@VinuP2023 4 года назад
@@SingletonSean awesome. Lot of learning opportunity. Thank you Sean 🙏
Далее
Adding Dependency Injection to WPF applications
22:42
▼ КАПИТАН НАШЁЛ НЕФТЬ В 🍑
33:40
Просмотров 260 тыс.
Background Tasks Are Finally Fixed in .NET 8
10:29
Просмотров 111 тыс.
▼ КАПИТАН НАШЁЛ НЕФТЬ В 🍑
33:40
Просмотров 260 тыс.