【发布时间】:2013-04-22 23:21:35
【问题描述】:
我正在使用 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 具有以下属性(日期、名称),其中名称属性应本地化;我构建了以下实体来表示实体及其本地化版本:
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 时应修改具有当前语言环境的名称的值。
我用 Razor 视图为 T 创建了一个控制器,并且没有任何进一步的修改,当导航到创建视图时,我得到了正确的视图,但是当单击创建按钮时,我遇到了方法“ TWithCurrentLocaleOrDefault”,因为我注意到它是在调用 setter 之前从 Name 属性的 getter 中调用的……并且由于刚刚创建的 T 实例没有本地化版本,因此我从方法中得到了异常。
我不知道我是否在这里遗漏了什么,或者我是否使用了错误的逻辑,所以请您向我解释一下哪里出了问题或指出一个很好的教程或示例代码来处理本地化使用MV。
更新:
问题是我正在创建多个 Context 实例,所以我想我应该更改在访问任何本地化实体的当前本地化实例时使用的逻辑(T 的当前 T_Locale),如果有任何好的逻辑要处理这种情况,请指点我...
再次抱歉,很长的问题,任何帮助将不胜感激,并提前非常感谢。
【问题讨论】:
标签: entity-framework asp.net-mvc-4 localization ef-code-first code-first