I know the association between language resources in RESX files and Entity Framework Core seems crazy but I have an issue with it. This is my scenario. I have a Persistence
class where I take care of the repositories and the seed of the database. In this project, I have a Dictionary1.resx with a list of words in English and a few others for localization in other languages. To simplify, I have the Dictionary1.resx and Dictionary1.es-ES.resx.
Now, I want to save the list of words in the database in both languages as a seed when the first time the application starts, and the database has not been created yet.
So, I call the ResourceManager
to assign a different culture
ResourceManager rm = new ResourceManager(
"Dictionary1", Assembly.GetExecutingAssembly());
CultureInfo esCulture = new CultureInfo("es-ES");
Then, I try to add the word using the relative CultureInfo
. For example:
public static void Seed(this ModelBuilder modelBuilder)
{
modelBuilder.Entity<Word>().HasData(
new Word
{
ID = 61,
WordName = rm.GetString("Actress", esCulture)
}
)
}
Now, I want to create the migration for the database, and I use the add-migration
for it. When I run this one, I get this error from Entity Framework Core.
Unable to create a ‘DbContext’ of type ”. The exception ‘Could not find the resource “Dictionary1.resources” among the resources “LIU.Persistence.Resources.Dictionary1.resources” embedded in the assembly “LIU.Persistence”, nor among the resources in any satellite assemblies for the specified culture. Perhaps the resources were embedded with an incorrect name.’ was thrown while attempting to create an instance. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728
If I comment the creation of the Word
, Entity Framework Core can create the migration code. In the following screenshot, you can see the error. The red rectangle is when I comment the creation of the Word
.
How can I read the localization from a different culture?
It has nothing to do with EF Core. The
baseName
argument of theResourceManager
constructor seems to be incorrect, should benew ResourceManager("LIU.Persistence.Resources.Dictionary1", ...)