2
我有这个简单的实体从数据库表 如何映射两个实体支持本地化到一个POCO在EF 4
但我想我的POCO类是只有两个类:
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; }
}
然后,我想为和Name
“City
”的本地化属性“LocalName
”由我的上下文填充。
那么如何将这个实体映射到POCO类?
有没有更好的方法来做到这一点?
或者我应该从实体手动转换到我的模型,并添加一个更多的层?
请帮我做决定。
。
:附加信息:
我做我的ObjectContext
对付我的POCO,但是当我试图填补我的其他财产“LocalName
”在库中我得到这个错误:
The entity or complex type 'Site.Country' cannot be constructed
in a LINQ to Entities query.
这是我的知识库中的方法:
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,
};
}
我不知道如何填写“LocalName
”属性LINQ声明
但EF无法处理您的'CityLocalized'类。但是我希望EF能够直接处理我的模型,这样我就可以查询并返回ViewModel,只需要那些不想从数据库中获取不必要数据的数据。 – 2010-10-31 11:15:48
ViewModel是这个问题的最佳选择。谢谢 :) – 2011-09-11 13:49:59