ResourceDictionary in Entity Framework Core - lucyberryhub/WPF.Tutorial GitHub Wiki
๐ ResourceDictionary in Entity Framework Core Without Crashing Migrations
"One day, Lucy Berry was happily coding her WPF application when suddenly... BOOM! โ Entity Framework Core threw an error: Object reference not set to an instance of an object. What happened?! ๐คฏ"
Lucy wanted to store localized strings in Strings.xaml
and use them inside her Entity Framework Core database seeding, but when she tried:
Spec = (string)Application.Current.Resources["str_koshou"];
๐ฅ ERROR! ๐ฅ Because EF Core migrations run in a non-WPF environment, Application.Current.Resources
is always null
.
But fear not! Lucy Berry found the ultimate solution that loads Strings.xaml
manually, making EF Core happy! Letโs go step by step. ๐
Application.Current.Resources
?
๐จ The Big Problem: Why Canโt EF Core Use add-migration
?
๐ What happens during - EF Core creates a
DbContext
without running WPF. - WPF resources (like
Strings.xaml
) donโt exist in this environment. Application.Current
isnull
, so accessingApplication.Current.Resources
crashes the app!
๐ด The Bad Code That Causes the Crash
Spec = (string)Application.Current.Resources["str_koshou"]; // โ Crash! Application.Current is null!
Strings.xaml
Manually!
โ
The Solution: Load ResourceHelper
to Load Strings.xaml
๐น Step 1: Create a Lucy created a special helper class that loads the Strings.xaml
file manually, even when WPF isnโt running.
ResourceHelper.cs
๐ using System;
using System.IO;
using System.Windows;
using System.Windows.Markup;
public static class ResourceHelper
{
private static ResourceDictionary _resourceDictionary;
static ResourceHelper()
{
LoadResourceDictionary();
}
private static void LoadResourceDictionary()
{
try
{
// Path to your Strings.xaml file (Update this if needed)
string resourcePath = "Fw/Resources/Strings.xaml";
using (FileStream fs = new FileStream(resourcePath, FileMode.Open, FileAccess.Read))
{
_resourceDictionary = (ResourceDictionary)XamlReader.Load(fs);
}
}
catch (Exception ex)
{
Console.WriteLine("โ Failed to load Strings.xaml: " + ex.Message);
}
}
public static string GetString(string key)
{
if (_resourceDictionary != null && _resourceDictionary.Contains(key))
{
return _resourceDictionary[key] as string;
}
return key; // ๐ Fallback to key if not found
}
}
What this code does:
โ
Loads Strings.xaml
manually so it can be accessed anywhere!
โ
Uses XamlReader.Load()
to parse the .xaml
file.
โ
Falls back to the key if the resource isnโt found.
ResourceHelper
in AppDbContext
๐น Step 2: Use Now, Lucy updated AppDbContext.cs
to use ResourceHelper.GetString(...)
instead of Application.Current.Resources
.
AppDbContext.cs
๐ modelBuilder.Entity<EnvObikiModel>(entity =>
{
entity.HasData(
new EnvObikiModel
{
Id = 1,
Title = "90่ง 4m",
Spec = ResourceHelper.GetString("str_koshou"), // โ
Works in EF Core migrations!
ComboValue = "็ฉๅฝข",
Dim1 = 90,
Dim2 = 90,
Dim3 = 4000,
Deleteable = 0
}
);
});
๐ Success! Now Strings.xaml
can be used inside EF Core migrations without crashing! ๐
๐ญ Story Time: Lucy Berry and the Magic Strings!
Lucy was working on FwCAD, an amazing WPF application that needed localized text inside an SQLite database. She added all her Japanese labels in Strings.xaml
:
<sys:String x:Key="str_koshou">ๅผ็งฐ</sys:String>
<sys:String x:Key="str_kankyosette">็ฐๅข่จญๅฎ</sys:String>
<sys:String x:Key="str_kiso">ๅบ็ค</sys:String>
Then she tried to insert these values into her database using Application.Current.Resources
. But when she ran:
dotnet ef migrations add InitialCreate
๐ฅ BOOM! CRASH! ERROR! โ
Lucy was sad. ๐ข But she didnโt give up! ๐ช She found a way to manually load Strings.xaml
and made EF Core work with localized text. Now her app can store ๅผ็งฐ (Koshou) inside the database and retrieve it later! ๐
๐น More Example Code
Lucy wanted to make sure other developers could also fetch localized text anywhere in the app. So she wrote more examples:
โ Use Localized Strings Anywhere in WPF
string title = ResourceHelper.GetString("str_kankyosette"); // Returns ็ฐๅข่จญๅฎ
Console.WriteLine(title);
โ Use Localized Strings in Data Binding
<TextBlock Text="{x:Static local:ResourceHelper.GetString('str_kiso')}" />
โ Use Localized Strings in ViewModel
public string Title => ResourceHelper.GetString("str_kankyosette");
๐ Summary: What Lucy Berry Learned
โ
EF Core migrations run without WPF, so Application.Current.Resources
is null.
โ
Strings.xaml
must be loaded manually using XamlReader.Load()
.
โ
A helper class (ResourceHelper.cs
) makes localized text available inside EF Core and WPF.
โ
This solution allows localization in the database without breaking migrations!
๐ The Final Victory!
Lucy Berry ran the migration again:
dotnet ef migrations add InitialCreate
dotnet ef database update
โ
No more errors!
โ
Localized text is stored in the database!
โ
Lucy is now an EF Core Localization Champion! ๐๐
๐ฉโ๐ป Want More?
๐ Read more about ResourceDictionary in WPF: Microsoft Docs
๐ Learn about Entity Framework Core Migrations: EF Core Docs