Sitecore Glass映射器GetItem< TypeName>(guid)始终返回null [英] Sitecore Glass mapper GetItem<TypeName>(guid) always return null
问题描述
我看到一个相关的问题: Sitecore Glass Mapper始终为空
I saw a related question: Sitecore Glass Mapper always null
但是不幸的是,这并不能为我的案件提供解决方案.
But unfortunately it does not give a solution for my case.
以下是代码段:
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为空
In the code above: i is not null t is null
Article是一个简单的类,例如:
Article is the simple class like:
[SitecoreType(TemplateId = "{4C4EC1DA-EB77-4001-A7F9-E4C2F61A9BE9}")]
public class Article
{
[SitecoreField(FieldName = "Title")]
public string Title { get; set; }
}
Sitecore中仅安装了一种语言-zh,在web.config中的各项中也已指定了该语言.
There are only one language installed in Sitecore - en, it has been specified in the web.config in the items as well.
我还在Global.asax.cs
的Application_Start
中添加了GlassMapperSc.Start();
,并通过var attributes = new AttributeConfigurationLoader(new[] { "Assembly.Name" });
将我的程序集添加到了包含的程序集列表中,我成功地在SitecoreContext
映射中找到了我的类.
Also I have added GlassMapperSc.Start();
to Application_Start
in the Global.asax.cs
and added my assembly to the list of included assemblies via var attributes = new AttributeConfigurationLoader(new[] { "Assembly.Name" });
and I succeeded to find my class in the SitecoreContext
mappings.
它看起来像不是语言问题,如开头提供的链接所述.而且我已经为此苦苦挣扎了很长时间,但是没有运气...
It does not looks like a language issue, as stated in the link provided in the very beginning. And I'm struggling with it already for a pretty long time, but no luck...
谢谢!
推荐答案
我刚刚注意到您将主数据库用于Sitecore数据库,将SitecoreContext
用于Glass.
I just noticed that you are using master db for the Sitecore DB and SitecoreContext
for Glass.
SitecoreContext
类将在运行时使用由Sitecore.Context.Database
属性定义的数据库.这可能意味着它正在使用Web数据库.
The SitecoreContext
class will use the database that is defined by the Sitecore.Context.Database
property at runtime. This probably means that it is using the web database.
您可以使用以下方法检查是否已将项目发布到Web数据库吗?
Can you check that you have published the item to the web database or instead using:
var context = new SitecoreService("master");
这篇关于Sitecore Glass映射器GetItem< TypeName>(guid)始终返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!