开发者

Localized tables and Entity Framework

开发者 https://www.devze.com 2022-12-10 07:10 出处:网络
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

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:

Localized tables and Entity Framework

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号