首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用EF的本地化实体-代码优先

使用EF的本地化实体-代码优先
EN

Stack Overflow用户
提问于 2013-04-29 10:17:19
回答 1查看 2.7K关注 0票数 4

我正在使用MVC与CodeFirst一起创建一个多语言网站,我遵循了本教程(http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/creating-an-entity-framework-data-model-for-an-asp-net-mvc-application),但我面临一些问题…

我试着详细解释一下,很抱歉…这个问题太长了

假设我有一个具有以下属性(日期、名称)的实体T,其中应该本地化name属性;我已经构建了以下实体来表示该实体及其本地化版本:

代码语言:javascript
复制
public class T
{
    #region Primitive Properties

    public int TID { get; set; }

    [DisplayFormat(DataFormatString = "{0:d}")]
    [Required]
    public DateTime DateCreated { get; set; }

    #endregion

    #region Localized Properties

    protected T_Locale TWithCurrentLocaleOrCreate
    {
        get
        {
            T_Locale t = this.T_Locales.SingleOrDefault(record => record.LocaleID == Locale.CurrentLocale.LocaleID);
            // If the object is not available with the current locale,
            // create it
            if (t == null)
            {
                t = new T_Locale
                {
                    Locale = Locale.CurrentLocale
                };

                this.T_Locales.Add(t);
            }

            return t;
        }
    }

    protected T_Locale TWithCurrentLocaleOrDefault
    {
        get
        {
            T_Locale t = this.T_Locales.SingleOrDefault(record => record.LocaleID == Locale.CurrentLocale.LocaleID);
            // If the object is not available with the current locale,
            // return it with the default locale
            if (t == null)
            {
                t = this.T_Locales.SingleOrDefault(record => record.LocaleID == Locale.DefaultLocale.LocaleID);
                // If the object is not available with the current locale,
                // return it with any available locale
                if (t == null)
                    t = this.T_Locales.First();
            }

            return t;
        }
    }

    [NotMapped]
    public string Name
    {
        get
        {
            return this.TWithCurrentLocaleOrDefault.Name;
        }
        set
        {
            this.TWithCurrentLocaleOrCreate.Name = value;
        }
    }

    #endregion

    #region Navigation Properties

    public virtual ICollection<T_Locale> T_Locales { get; set; }

    #endregion
}


public class T_Locale
{
    #region Primitive Properties

    [Key]
    [Column(Order = 0)]
    public int TID { get; set; }

    [Key]
    [Column(Order = 1)]
    public int LocaleID { get; set; }

    [Required]
    public string Name { get; set; }

    #endregion

    #region Navigation Properties

    public virtual T T { get; set; }

    public virtual Locale Locale { get; set; }

    #endregion
}


public class Locale
{
    #region Primitive Properties

    public int LocaleID { get; set; }

    [Required]
    public string Name { get; set; }

    [Required]
    public string DisplayName { get; set; }

    #endregion

    #region Navigation Properties

    public virtual ICollection<T_Locale> T_Locales { get; set; }

    #endregion

    #region Current Locale Properties

    protected static string DefaultLocaleName
    {
        get
        {
            return "en";
        }
    }

    private static Locale _DefaultLocale;
    public static Locale DefaultLocale
    {
        get
        {
            DatabaseContext database = new DatabaseContext();
            _DefaultLocale = database.Locales.SingleOrDefault(record => record.Name == Locale.DefaultLocaleName);

            return _DefaultLocale;
        }
    }

    private static Locale _CurrentLocale;
    public static Locale CurrentLocale
    {
        get
        {
            DatabaseContext database = new DatabaseContext();

            if (_CurrentLocale == null)
            {
                // To Avoid the large logic behind for getting the current locale I’m using the default one here…
                _CurrentLocale = Locale.DefaultLocale;
            }

            return _CurrentLocale;
        }
    }

    #endregion
}

其中T:是我感兴趣的实体,T_Locale是T的本地化版本,它只包含应该本地化的属性,正如您可能注意到的,我已经用T(名称)编写了一个NotMapped属性,以获得属性名称…的当前本地化版本此属性应返回具有当前区域设置的名称,并且在调用setter时,应修改具有当前区域设置的名称的值。

我已经为T创建了一个带有Razor视图的控制器,并且没有做任何修改,当导航到create视图时,我得到了正确的视图,但是当单击create按钮时,我从方法“TWithCurrentLocaleOrDefault”中得到了一个异常,因为我注意到在调用setter…之前,它是从名称属性的getter调用的。由于刚刚创建的T实例没有本地化版本,所以我从方法中获得了异常。

我不知道我在这里是不是遗漏了什么,或者我是否使用了错误的逻辑,所以请您向我解释一下哪里出了问题,或者告诉我如何使用Mvc处理本地化的好教程或示例代码。

更新:

问题是我正在创建多个上下文实例,所以我想我应该改变我在访问任何本地化实体的当前本地化实例(当前的T_Locale of T)时使用的逻辑,如果有任何好的逻辑来处理这种情况,请告诉我.

对于这个非常长的问题,我再次表示歉意,任何帮助都将不胜感激,并提前表示感谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-04-29 10:23:45

我决定使用Griffin本地化,因为它非常容易实现和管理。关于代码项目有一个非常好的教程:基于ASP.NET MVC的Griffin.MvcContrib定位

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16276174

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档