C# Fluent NHibernate从基类正确映射只读属性
我想为我的一些实体创建一个基类,因为它们都共享一个C# Fluent NHibernate从基类正确映射只读属性,c#,list,nhibernate,fluent-nhibernate,readonly-collection,C#,List,Nhibernate,Fluent Nhibernate,Readonly Collection,我想为我的一些实体创建一个基类,因为它们都共享一个事件列表属性。 我还想使事件列表成为只读属性 因此,我创建了一个基本的EventRelatedEntity类,然后在与事件相关的每个实体类上派生它 还请注意,EventRelatedEntity类没有NHibernate映射类,因为它没有链接到表 请参阅下面的代码 基类: public class EventRelatedEntity { private readonly List<Event> events; pu
事件
列表属性。
我还想使事件
列表成为只读属性
因此,我创建了一个基本的EventRelatedEntity
类,然后在与事件相关的每个实体类上派生它
还请注意,EventRelatedEntity
类没有NHibernate映射类,因为它没有链接到表
请参阅下面的代码
基类:
public class EventRelatedEntity
{
private readonly List<Event> events;
public virtual IReadOnlyCollection<Event> Events { get; protected set; }
public EventRelatedEntity()
{
events = new List<Event>();
Events = events.AsReadOnly();
}
protected virtual void AddEvent<T>(T entity, string message)
{
if (events == null)
events = new List<Event>();
Event newEvent = new Event();
if (typeof(T) == typeof(Company))
{
newEvent.CompanyId = (entity as Company).Id;
// ...and do some other stuff...
}
else if (typeof(T) == typeof(Document))
{
newEvent.DocumentId = (entity as Document).Id;
// ...and do some other stuff...
}
else if (typeof(T) == typeof(Typology))
{
newEvent.TypologyId = (entity as Typology).Id;
// ...and do some other stuff...
}
newEvent.Message = message;
events.Add(newEvent);
}
}
public class EventRelatedEntity
{
private readonly IList<Event> events;
public virtual IReadOnlyCollection<Event> Events { get; protected set; }
public EventRelatedEntity()
{
events = new List<Event>();
Events = (events as List<Event>).AsReadOnly();
}
// ...
}
实体的流畅NHibernate映射类
public class Company : EventRelatedEntity
{
[Key]
public virtual int Id { get; protected set; }
[Required]
public virtual string Alias { get; set; }
[Required]
public virtual string CompanyName { get; set; }
// ...and some other properties...
#region Actions
public virtual void AddEvent(string message)
{
base.AddEvent(this, message);
}
#endregion
}
public class Document : EventRelatedEntity
{
[Key]
public override int Id { get; protected set; }
[Required]
public virtual User User { get; protected set; }
// ...and some other properties...
#region Actions
public virtual void AddEvent(string message)
{
base.AddEvent(this, message);
}
#endregion
}
// ...and some other classes...
public class CompanyMap : ClassMap<Company>
{
public CompanyMap()
{
Table("Companies");
LazyLoad();
Id(x => x.Id).GeneratedBy.Identity().Column("Id");
Map(x => x.Alias).Column("Alias").Not.Nullable();
Map(x => x.CompanyName).Column("CompanyName").Not.Nullable();
// ...and some other mappings...
// Link with Events table
HasMany(x => x.Events) // Events is declared in the base class (EventRelatedEntity)
.KeyColumn("CompanyId")
.Access.LowerCaseField()
.Cascade.AllDeleteOrphan();
}
}
public class DocumentMap : ClassMap<Document>
{
public DocumentMap()
{
Table("Documents");
LazyLoad();
Id(x => x.Id).GeneratedBy.Identity().Column("Id");
References(x => x.User).Column("UserId");
// ...and some other mappings...
// Link with Events table
HasMany(x => x.Events) // Events is declared in the base class (EventRelatedEntity)
.KeyColumn("DocumentId")
.Access.LowerCaseField()
.Cascade.AllDeleteOrphan();
}
}
// ...and some other mapping classes...
问题是,当我执行以下操作时:
Document document = session.Get<Document>(1);
使用列表接口而不是具体的列表类,那么
NHibernate.Collection.Generic.PersistentGenericBag的类型转换应该可以工作
使用IList
而不是List
以便EventRelatedEntity
成为:
public class EventRelatedEntity
{
private readonly IList<Event> events;
// rest of implementation...
}
公共类EventRelatedEntity
{
私有只读IList事件;
//其余的实现。。。
}
那么问题出在哪里?如果Events
确实声明为IReadOnlyCollection
,则document.Events.Add(new Event())
应生成错误:'IReadOnlyCollection'不包含“添加”的定义…
我收到一个NHibernate错误。我更新了问题。
public class EventRelatedEntity
{
private readonly IList<Event> events;
// rest of implementation...
}