Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Entity framework 首先使用EF代码的本地化实体_Entity Framework_Asp.net Mvc 4_Localization_Ef Code First_Code First - Fatal编程技术网

Entity framework 首先使用EF代码的本地化实体

Entity framework 首先使用EF代码的本地化实体,entity-framework,asp.net-mvc-4,localization,ef-code-first,code-first,Entity Framework,Asp.net Mvc 4,Localization,Ef Code First,Code First,我正在使用带有CodeFirst的MVC创建一个多语言网站,我遵循了本教程()但我面临一些问题 我会详细解释的,很抱歉问了这么长的问题 假设我有一个具有以下属性(日期、名称)的实体T,其中名称属性应该本地化;我构建了以下实体来表示实体及其本地化版本: public class T { #region Primitive Properties public int TID { get; set; } [DisplayFormat(DataFormatString = "

我正在使用带有CodeFirst的MVC创建一个多语言网站,我遵循了本教程()但我面临一些问题

我会详细解释的,很抱歉问了这么长的问题

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

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
{
#区域基本属性
公共int TID{get;set;}
[DisplayFormat(DataFormatString=“{0:d}”)]
[必需]
public DateTime DateCreated{get;set;}
#端区
#区域本地化特性
受保护的T_Locale TWithCurrentLocaleOrCreate
{
得到
{
T_Locale T=this.T_Locales.SingleOrDefault(record=>record.LocaleID==Locale.CurrentLocale.LocaleID);
//如果对象在当前区域设置中不可用,
//创造它
如果(t==null)
{
t=新的t_语言环境
{
Locale=Locale.CurrentLocale
};
this.T_Locales.Add(T);
}
返回t;
}
}
受保护的T_区域设置TwithCurrentLocaleOrderFault
{
得到
{
T_Locale T=this.T_Locales.SingleOrDefault(record=>record.LocaleID==Locale.CurrentLocale.LocaleID);
//如果对象在当前区域设置中不可用,
//使用默认语言环境返回它
如果(t==null)
{
t=this.t_Locales.SingleOrDefault(record=>record.LocaleID==Locale.DefaultLocale.LocaleID);
//如果对象在当前区域设置中不可用,
//将其与任何可用的区域设置一起返回
如果(t==null)
t=this.t_Locales.First();
}
返回t;
}
}
[未映射]
公共字符串名
{
得到
{
返回this.twithCurrentLocaleOrderFault.Name;
}
设置
{
this.TWithCurrentLocaleOrCreate.Name=值;
}
}
#端区
#区域导航属性
公共虚拟ICollection T_Locales{get;set;}
#端区
}
公共类T_语言环境
{
#区域基本属性
[关键]
[第列(顺序=0)]
公共int TID{get;set;}
[关键]
[第列(顺序=1)]
public int LocaleID{get;set;}
[必需]
公共字符串名称{get;set;}
#端区
#区域导航属性
公共虚拟T{get;set;}
公共虚拟区域设置区域设置{get;set;}
#端区
}
公共类语言环境
{
#区域基本属性
public int LocaleID{get;set;}
[必需]
公共字符串名称{get;set;}
[必需]
公共字符串DisplayName{get;set;}
#端区
#区域导航属性
公共虚拟ICollection T_Locales{get;set;}
#端区
#区域当前区域设置属性
受保护的静态字符串DefaultLocaleName
{
得到
{
返回“en”;
}
}
私有静态语言环境_DefaultLocale;
公共静态区域设置DefaultLocale
{
得到
{
DatabaseContext数据库=新建DatabaseContext();
_DefaultLocale=database.Locales.SingleOrDefault(记录=>record.Name==Locale.DefaultLocaleName);
返回_DefaultLocale;
}
}
私有静态语言环境\u CurrentLocale;
公共静态区域设置CurrentLocale
{
得到
{
DatabaseContext数据库=新建DatabaseContext();
如果(_CurrentLocale==null)
{
//为了避免获取当前语言环境的大逻辑,我在这里使用默认语言环境…
_CurrentLocale=Locale.DefaultLocale;
}
返回_CurrentLocale;
}
}
#端区
}
其中T:是我感兴趣的实体,T_Locale是T的本地化版本,它只包含应该本地化的属性,您可能会注意到,我在T(Name)中编写了一个NotMapped属性,以获取属性名称的当前本地化版本…此属性应返回带有当前区域设置的名称,调用setter时,应修改当前区域设置的名称值

我已经用Razor视图为T创建了一个控制器,没有任何进一步的修改,当导航到create视图时,我得到了正确的视图,但当单击create按钮时,我遇到了来自方法“TwithCurrentLocaleOrderFault”的异常正如我所注意到的,在调用setter之前,它是从Name属性的getter调用的……由于刚刚创建的T实例没有本地化版本,因此我从该方法中得到了异常

我不知道我是否遗漏了什么,或者我是否使用了错误的逻辑,所以请您向我解释什么是错误的,或者为我指出一个好的教程或示例代码,以使用Mvc处理本地化

更新:

问题是我正在创建多个上下文实例,所以我想我应该更改访问任何本地化实体的当前本地化实例(当前T_语言环境T)时使用的逻辑,如果有任何好的逻辑来处理这种情况,请指向我


对于这个很长的问题再次表示抱歉,我们非常感谢您提供的任何帮助。

我决定使用Griffin本地化,因为它非常易于实施和管理。关于代码项目有一个非常好的教程:

我确实读过这篇文章,但我不知道它如何帮助我存储任何可本地化实体所需的本地化版本。。。我需要一个包含每个实体翻译的数据库表,因为