开发者

How to Support Localization by mapping two entities to one POCO in EF 4

I've this simple Entities from DB tables

How to Support Localization by mapping two entities to one POCO in EF 4

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<Count开发者_运维技巧ry> 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

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜