C# 是否可以将CategoryID值指定给CategoryInfo对象?
我一直在尝试使用以下代码块添加一个新类别:C# 是否可以将CategoryID值指定给CategoryInfo对象?,c#,kentico,C#,Kentico,我一直在尝试使用以下代码块添加一个新类别: CategoryInfo category = new CategoryInfo() { CategoryID = 999, // manually set CategoryName = "TestCategory", CategoryDisplayName = "Test Category", CategoryEnabled = true, CategorySiteID = 1 }; CategoryInfo
CategoryInfo category = new CategoryInfo()
{
CategoryID = 999, // manually set
CategoryName = "TestCategory",
CategoryDisplayName = "Test Category",
CategoryEnabled = true,
CategorySiteID = 1
};
CategoryInfoProvider.SetCategoryInfo(category);
这不会引发任何错误,但不会将新类别添加到CMS\u category
表中
但是,如果我删除了这一行:CategoryID=999,
,则该类别将保存到系统中,并自动分配CategoryID
我想手动设置此字段。感谢您的帮助。
(我试图避免创建其他字段来处理此问题)我还没有尝试过,但您可以尝试在创建后更改类别的ID,例如:
[CustomCategoryID]
public partial class CMSModuleLoader
{
private class CustomCategoryID : CMSLoaderAttribute
{
public override void Init()
{
CategoryInfo.TYPEINFO.Events.Insert.After += Insert_After;
}
void Insert_After(object sender, CMS.DataEngine.ObjectEventArgs e)
{
var category = e.Object as CategoryInfo;
if (category != null)
{
category.CategoryID = 999, // manually set
category.Update();
}
}
}
}
- 您确定系统没有存储您的新类别,而是使用不同的ID(您提供的ID)存储吗
- 您能告诉我为什么要自定义对象ID行为吗?谢谢
CategoryID
,系统实际上会调用updatecms\u Category set。。。其中CategoryID=@CategoryID
而不是插入到…
如果您需要存储原始引用(我猜您是为了集成目的而尝试存储外部标识符),我建议将其存储在单独的字段中。为了防止修改系统字段,我将使用code name字段-CategoryName
尽管@martin的建议似乎是个好主意,但我很确定它不会起作用,因为Kentico会丢失对当前正在更新的对象的引用。如果您有有效的源代码许可证,请查看
CategoryInfoProvider.SetCategoryInfoInternal()
以了解我在说什么。Hi@martin,我有一些记录需要保存到数据库中,其中categoryID和CategoryParentID是预定义的。我需要设置CategoryID,因为我必须设置ParentCategoryID的依赖项。我试图避免额外的代码行,以创建一个新的映射来设置类别和子类别。是的,这只是猜测。我已经试过了,但它不起作用(因为提到的原因)。谢谢@rocky,是的,我正在尝试存储外部ID。我将尝试使用categoryName字段存储此标识符。