我使用FluentNHibernate(Automapping)進行映射,NHibernate 3.2用於數據訪問,SchemaExport用於生成我的數據庫。NHibernate:JoinedSubclass,HasMany
我有一個類Principal
這是User
和Usergroup
的基類。 Principal
有一個屬性CommonThing
類型CommonThing
。 CommonThing
有2組:ManagedUsers
和ManagedUsergroups
。
現在爲Principals
-table(OK),Users
-table(WRONG),Usergroups
-table(WRONG)生成了一列CommonThingId
。
如何獲得FluentNHibernate到只生成Principals
-table中的列而不是子類別表?
編輯:類&映射 校長:
public abstract class Principal : Entity
{
...
public virtual CommonThing CommonThing
{
get
{
return _commonThing;
}
set
{
if (_commonThing == value)
return;
_commonThing = value;
if (_commonThing == null)
return;
if (this is Usergroup)
_commonThing.AddUsergroup(this as Usergroup);
else if (this is User)
_commonThing.AddUser(this as User);
}
}
...
}
用戶:
public partial class User : Principal
{
...
}
用戶組:
public partial class Usergroup : Principal
{
...
}
CommonThing:
public class CommonThing : Entity
{
...
public virtual IEnumerable<User> ManagedUsers { get { return _managedUsers; } set { _managedUsers = (Iesi.Collections.Generic.ISet<User>)value; } }
public virtual IEnumerable<Usergroup> ManagedUsergroups { get { return _managedUsergroups; } set { _managedUsergroups = (Iesi.Collections.Generic.ISet<Usergroup>)value; } }
...
}
公約:
public class ReferenceConvention : IReferenceConvention
{
public void Apply(IManyToOneInstance instance)
{
var keyName = string.Format(CultureInfo.InvariantCulture, "FK_MtO_{0}_in_{1}_{2}",
instance.Property.PropertyType.Name,
instance.EntityType.Name,
instance.Name);
instance.ForeignKey(keyName);
instance.LazyLoad();
instance.Cascade.SaveUpdate();
instance.Column(instance.Property.PropertyType.Name + "Id");
instance.Access.CamelCaseField(CamelCasePrefix.Underscore);
}
}
public class ForeignKeyConvention : FluentNHibernate.Conventions.ForeignKeyConvention
{
protected override string GetKeyName(Member property, Type type)
{
if (property == null)
return type.Name + "Id";
return property.Name + "Id";
}
}
public class HasManyConvention : IHasManyConvention
{
public void Apply(IOneToManyCollectionInstance instance)
{
var keyName = string.Format(CultureInfo.InvariantCulture, "FK_OtM_{0}_{1}2{2}",
instance.Member.ReflectedType.Name,
instance.Member.Name,
instance.EntityType.Name);
instance.Key.ForeignKey(keyName);
if(instance.Key.Columns.Count() != 0)
instance.Inverse();
instance.Cascade.SaveUpdate();
instance.Cache.ReadWrite();
instance.Cache.IncludeAll();
instance.Access.CamelCaseField(CamelCasePrefix.Underscore);
}
}
public class JoinedSubclassConvention : IJoinedSubclassConvention
{
public void Apply(IJoinedSubclassInstance instance)
{
instance.Table("" + Inflector.Net.Inflector.Pluralize(instance.Type.Name));
instance.Key.Column("Id");
instance.DynamicInsert();
instance.DynamicUpdate();
instance.LazyLoad();
}
}
主體映射:
public class PrincipalMapping : IAutoMappingOverride<Principal>
{
public void Override(AutoMapping<Principal> mapping)
{
...
mapping.References(x => x.CommonThing)
.LazyLoad()
.Nullable()
.Access.CamelCaseField(Prefix.Underscore)
.Cascade.None();
;
mapping.JoinedSubClass<User>("Id");
mapping.JoinedSubClass<Usergroup>("Id");
...
}
}
CommonThing映射:
public class CommonThingMapping : IAutoMappingOverride<CommonThing>
{
public void Override(AutoMapping<CommonThing> mapping)
{
...
mapping.HasMany(x => x.ManagedUsers)
.AsSet()
.ExtraLazyLoad()
;
mapping.HasMany(x => x.ManagedUsergroups)
.ExtraLazyLoad()
.AsSet()
;
...
}
}
了Lg
warappa
請張貼代碼。我們無法告訴您如何解決「錯誤」而無需任何代碼。 –
對不起,我現在添加了必要的信息。 –