Orchard 自定义模块 - NHibernate 拾取模型 - 需要虚拟属性
我正在 Orchard CMS 中构建自定义模块,并且我有以下控制器操作:
public ActionResult Inventory()
{
var models = _repository.Get<MyModel>();
return View(new MyViewModel() { MyModels = models.ToList() });
}
现在,当 Orchard 构建此模块时,会发生异常,因为 NHibernate 已拾取 MyModel 因为它被视为 PartRecord,我这样做不希望如此。
例外情况是方法 get_Id 应该是“公共/受保护的虚拟”或“受保护的内部虚拟”。
我可以将 virtual 添加到属性中,但我想避免让 NHibernate 参与查看此对象,这是不必要的。
我的问题是,如何添加模型、在视图中访问该模型并禁止 Orchard 将其视为 PartRecord?实际上,我在一个独立的存储库中使用该模型,与 Orchard DB 分开。
编辑:附加信息
_repository 是一个 IDbRepository,指向我自己的数据库实例 MyModel 是我的数据的 POCO。我试图避免在 MyModel 中的属性上放置 virtual,我将它们放在这里,以说明我在哪里试图避免它们。
public interface IDbRepository : IDependency
{
T Find<T>(long id) where T : class, IActiveRecord;
bool Update<T>(T record) where T : class, IActiveRecord;
bool Remove<T>(T record) where T : class, IActiveRecord;
}
public class MyModel : IActiveRecord
{
[Key]
public virtual long Id { get; set; }
public virtual int SupplierProductId { get; set; }
public virtual int SupplierId { get; set; }
public virtual int ProductId { get; set; }
}
除此之外,我的模块确实有一个我创建的设置部分,它存储在 Orchard DB 中并在站点设置中进行编辑。我正在使用处理程序来控制它。然而,我尝试删除处理程序和其他一些东西,以防止 Orchard 将我的对象带入 NHibernate。
I'm working on building a custom module in Orchard CMS, and I have the following Controller Action:
public ActionResult Inventory()
{
var models = _repository.Get<MyModel>();
return View(new MyViewModel() { MyModels = models.ToList() });
}
Now, when Orchard builds this, an exception occurs, because NHibernate has picked up MyModel because its being looked at as a PartRecord, which I do not want it to be.
The exception is method get_Id should be 'public/protected virtual' or 'protected internal virtual'.
I can add virtual to the properties, but I want to avoid having NHibernate have any part in looking at this object, it is unnecessary.
My question is, how can I add a Model, access that model in a view and suppress Orchard from treating it as a PartRecord? I actually use the model in a separate repository, separate from the Orchard DB.
EDIT: Additional Information
_repository is an IDbRepository, pointing to my own DB instance
MyModel is a POCO for my data. I'm trying avoid putting virtual on the properties in MyModel, I have them in here, to illustrate where I'm trying to avoid them.
public interface IDbRepository : IDependency
{
T Find<T>(long id) where T : class, IActiveRecord;
bool Update<T>(T record) where T : class, IActiveRecord;
bool Remove<T>(T record) where T : class, IActiveRecord;
}
public class MyModel : IActiveRecord
{
[Key]
public virtual long Id { get; set; }
public virtual int SupplierProductId { get; set; }
public virtual int SupplierId { get; set; }
public virtual int ProductId { get; set; }
}
In addition to this, my module does have a settings part that I've created, that does get stored in Orchard DB and is edited in the site settings. I'm using a handler to control that. However, I've tried removing the handler and a few other things to keep Orchard from taking my object into NHibernate.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Orchard 自动映射配置假定,如果满足以下条件,任何类都将被映射:
Id
属性,带有虚拟访问IContent
或继承自ContentPartRecord
因此,如果您阻止任何这些条件为真,那么您应该做得很好。例如,在不以
.Models
或.Records
结尾的命名空间中定义类就足够了。Orchard auto-mapping configuration assumes that any class will be mapped if the following conditions are met:
Id
property, with virtual accessorsIContent
or inherits fromContentPartRecord
So if you prevent any of those criteria from being true, you should be good. For instance, defining your classes in a namespace not ending with
.Models
or.Records
should be sufficient.