【发布时间】:2014-07-02 00:21:38
【问题描述】:
我看到了一个相关的问题: Sitecore Glass Mapper always null
但不幸的是,它并没有为我的情况提供解决方案。
这里有一个代码 sn-p:
var db = Factory.GetDatabase("master");
var context = new SitecoreContext();
// the ID of Needed item
var g = new Guid("{F21C04FE-8826-41AB-9F3C-F7BDF5B35C76}");
// just to test if it's possible to fetch item using db.GetItem
var i = db.GetItem(new ID(g), Language.Current, Sitecore.Data.Version.Latest);
// Grab item
var t = context.GetItem<Article>(g);
在上面的代码中: 我不为空 t 为空
Article 是一个简单的类,例如:
[SitecoreType(TemplateId = "{4C4EC1DA-EB77-4001-A7F9-E4C2F61A9BE9}")]
public class Article
{
[SitecoreField(FieldName = "Title")]
public string Title { get; set; }
}
Sitecore 中只安装了一种语言 - en,它也已在 web.config 中的项目中指定。
我还在Global.asax.cs 中将GlassMapperSc.Start(); 添加到Application_Start,并通过var attributes = new AttributeConfigurationLoader(new[] { "Assembly.Name" }); 将我的程序集添加到包含的程序集列表中,并且我成功地在SitecoreContext 映射中找到了我的课程。
这看起来不像是语言问题,正如一开始提供的链接中所述。而且我已经为此苦苦挣扎了很长时间,但没有运气...
谢谢!
【问题讨论】:
-
只是为了确定,您是否检查过您的项目
{F21C04FE-8826-41AB-9F3C-F7BDF5B35C76}的版本是否有效? -
使用标准的 Sitecore API 获取项目并检查它是否使用您期望的语言并且还有一个版本。我已经看到(并且无缘无故)SC 使用与您认为应该使用的语言不同的语言的问题。
-
@michael-edwards 我查了一下,它有语言'en'和版本'1'。
-
您是否尝试使用 "i.GlassCast
(true,true)" 直接投射 (i) ? “标题”也需要是虚拟的。 -
@ahmed-okour 谢谢! i.GlassCast
(true,true) 并向标题添加虚拟对我有帮助,但 GetItem (g) 由于某种原因仍然返回 null。
标签: orm sitecore code-generation glass-mapper tds