如何通过映射两个实体支持本地化的一个POCO在EF 4 [英] How to Support Localization by mapping two entities to one POCO in EF 4
问题描述
我有这个简单的实体从数据库表
但是我希望我的POCO类是只有两个类:
公共类国家
{
公共字符串ID {获得;组; }
公共字符串名称{;组; }
公共字符串的localName {获得;组; } //这个本地化
公共字符串区域{获得;组; }
公开的IEnumerable<市>城市{获得;组; }
}
公共类城市
{
公众诠释ID {获得;组; }
公共字符串CountryID {获得;组; }
公共字符串名称{;组; } //这个本地化
大众双?经度{获得;组; }
大众双?纬度{获得;组; }
}
然后我想本地化属性的localName
为国家
和名称
为城
由我的角度来填补。
那么如何映射这个实体到POCO类?
和有没有更好的方式来做到这一点?
或者我应该做手工,从实体转换成我的模型,并添加多一个层次?
请帮我把决定。
:附加信息:的
我做了我的的ObjectContext
来对付我的POCO,但是当我尝试填补我的其他财产的localName
在库中我得到这个错误:
的实体或复杂类型Site.Country'不能建造
在LINQ到实体查询。
这是我的存储库中的方法:
公开的IQueryable<国家> GetCountries()
{
从国家context.Countries返回
加入countryCul在context.CountriesCultures
在国家code等于countryCul.CountryID
其中,countryCul.LangID ==恩
选择新的国家
{
code =国家code,
的localName = countryCul.LocalName,
名称= country.Name,
地区= country.Region,
};
}
我不知道怎么填写的localName
中的LINQ声明性
如果你想与ViewModel类的解决方案,你可以拿起这些:
有关包装城与它的选择LocalLanguage
公共类CityLocolized
{
公共市市{获得;组; }
公共字符串的localName {获得;组; }
}
在这里,我们选择了默认的或者有这样如果没有其他人加入,否则从CityCulture表
选择 字符串LANG = System.Threading.Thread.CurrentThread.CurrentUICulture.Name;
名单< CityLocolized> citiesLocalized = NULL;
如果(郎==恩)//这是默认的数据库
{
citiesLocalized =(从C在fke.Cities
选择新CityLocolized
{
市= C,
的localName = c.Name
}
).ToList();
}
否则//对于其他语言
{
citiesLocalized =(从C在fke.Cities
加入CC在fke.CityCultures
在c.Id等于cc.CityId
其中,cc.LangId ==郎
选择新CityLocolized
{
市= C,
的localName = cc.LocalName
})了ToList()。
}
和这一个传送的观点:
计算机[CitiesLocolized] =新的SelectList(citiesLocalized,City.Id,的localName);
终于观点:
<%:Html.DropDownListFor(型号=> model.CityId,计算机[CitiesLocolized]作为的SelectList)%>
我觉得就是它了。
moguzalp
I've this simple Entities from DB tables
But i want my POCO classes to be just two classes:
public class Country
{
public string ID { get; set; }
public string Name { get; set; }
public string LocalName { get; set; } // this localized
public string Region { get; set; }
public IEnumerable<City> Cities { get; set; }
}
public class City
{
public int ID { get; set; }
public string CountryID { get; set; }
public string Name { get; set; } //this localized
public double? Longitude { get; set; }
public double? Latitude { get; set; }
}
Then i want Localized properties "LocalName
for the Country
and Name
for the City
" to be filled by my context.
So how to map this Entities to POCO Classes?
And is there better way to do that?
Or i should make manual converting from Entities to my Model and add one more tier ?
Please help me to take decision .
.
: Additional information:
I made my ObjectContext
to deal with my POCO but when i try to fill my Additional property "LocalName
" in repository i get this error:
The entity or complex type 'Site.Country' cannot be constructed
in a LINQ to Entities query.
and this is the method in my Repository:
public IQueryable<Country> GetCountries()
{
return from country in context.Countries
join countryCul in context.CountriesCultures
on country.Code equals countryCul.CountryID
where countryCul.LangID == "en"
select new Country
{
Code = country.Code,
LocalName = countryCul.LocalName,
Name = country.Name,
Region = country.Region,
};
}
I don't know how to fill "LocalName
" property within linq statement
if you want a solution with ViewModel class, you can pick up these:
For wrapping City with it's selected LocalLanguage
public class CityLocolized
{
public City City { get; set; }
public String LocalName { get; set; }
}
Here, we chose the either default so with if else no join, else chose from CityCulture table
string lang = System.Threading.Thread.CurrentThread.CurrentUICulture.Name;
List<CityLocolized> citiesLocalized = null;
if (lang == "en") // which is default in db
{
citiesLocalized = (from c in fke.Cities
select new CityLocolized
{
City = c,
LocalName = c.Name
}
).ToList();
}
else // for other languages
{
citiesLocalized = (from c in fke.Cities
join cc in fke.CityCultures
on c.Id equals cc.CityId
where cc.LangId == lang
select new CityLocolized
{
City = c,
LocalName = cc.LocalName
}).ToList();
}
and this one for passing View:
ViewData["CitiesLocolized"] = new SelectList(citiesLocalized, "City.Id", "LocalName");
finally at view:
<%:Html.DropDownListFor(model => model.CityId, ViewData["CitiesLocolized"] as SelectList)%>
i think this is it.
moguzalp
这篇关于如何通过映射两个实体支持本地化的一个POCO在EF 4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!