简体   繁体   中英

Localized tables and Entity Framework

I have a scenario where I need to localized values of objects in my database.

Let's say you have an application that can create animals, if the user is english the value of the "Name" property of an animal would be entered as "Cat" in the UI whereas it would be entered as "Chat" in french.

The animal culture table would contain 2 records pointing to the same animal in the parent table.

When reading values back, if the value of "Name" does not exist in the user culture the default value (value the object was originally created with) would be used. The following diagrams demonstrate how the data is stored in SQL:

替代文字

I'm trying to map this schema to an object model using the Entity Framework, I'm a bit confused as to what the best way to approach the problem.

Is EF appropriate for this? Should I used EF4?

This EF model will be used by .NET RIA Services.

Thanks,

Pierre-Yves Troel

What I did in a similar situation is created a view say LocalizedAnimals which is a flat representation of that 2 table structure and created an EF model for that view. So when I need to display say French animal data I would filter those LocalizedAnimals and have nice simple object list as a result.

Something like this:

var localizedAnimals = myContext.LocalizedAnimals.Where(
                           p => p.CultureName == Thread.CurrentThread.CurrentUICulture.Name
                       );

I'm not sure you'd want to do this in the database. I think it would be more sensible to use a configuration file or resource that defines Culture-specific names.

You might also check Microsoft's documentation on internationalization and localization .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM