流畅的NHibernate自动映射,简单的问题,为外键关系添加额外的字段
如果我没有任何意义,我很抱歉我的大脑现在已经烧焦了。我刚刚开始在 NHibernate 中编程,由于自动映射,决定转向 Fluent Nhibernate,但我遇到了一些麻烦,这是它应该具有的关系:
这是 Fluent Nhibernate 自动映射的关系,注意 MemberId 已添加到表中:
我猜这是因为在我的约定中关系被设置为相反。我需要使关系为 inverse() ,否则级联不起作用。以下是我的约定:
public class HasManyConvention : IHasManyConvention
{
public void Apply(FluentNHibernate.Conventions.Instances.IOneToManyCollectionInstance instance)
{
instance.Key.Column(instance.EntityType.Name + "ID");
//instance.Inverse();
if (instance.Member.GetCustomAttributes(typeof(CascadeAttribute), false).Length <= 0)
{
instance.Cascade.None();
return;
}
var cascadeOption = (CascadeAttribute)instance.Member.GetCustomAttributes(typeof(CascadeAttribute), false)[0];
switch (cascadeOption.CascadeOption)
{
case Enums.CascadeOptions.All:
instance.Cascade.All();
break;
case Enums.CascadeOptions.AllDeleteOrphan:
instance.Cascade.AllDeleteOrphan();
break;
case Enums.CascadeOptions.Delete:
instance.Cascade.All();
break;
case Enums.CascadeOptions.DeleteOrphan:
instance.Cascade.DeleteOrphan();
break;
case Enums.CascadeOptions.None:
instance.Cascade.None();
break;
case Enums.CascadeOptions.SaveUpdate:
instance.Cascade.SaveUpdate();
break;
}
}
}
public class ReferenceConvention : IReferenceConvention
{
public void Apply(FluentNHibernate.Conventions.Instances.IManyToOneInstance instance)
{
instance.Column(instance.Property.Name + "Fk");
if (Attribute.IsDefined(instance.Property.PropertyType.Assembly, typeof(DomainSignatureAttribute)))
instance.UniqueKey("DomainSignature");
else
instance.Index(instance.Property.Name + "Index");
}
}
以及我的两个实体:
public class Member : Entity
{
public Member()
{
Mail = new List<Mail>();
}
[Cascade(Enums.CascadeOptions.All)]
public virtual IList<Mail> Mail
{
get; set;
}
}
public class Mail : Entity
{
public virtual Member Receiver
{
get; set;
}
public virtual Member Sender
{
get; set;
}
public virtual string Subject
{
get;
set;
}
public virtual string Body
{
get; set;
}
}
当很明显 Receiver 和 Sender 是成员表的外键时,为什么 Fluent nhibernate 会映射 MemberId?
Im sorry if I don't make any sense my brain is fried right now. I just started programming in NHibernate, decided to move to Fluent Nhibernate because of the automapping but I'm having a little trouble, here is the relation it should have:
Here is the relation Fluent Nhibernate's automapping made, notice that MemberId is added to the table:
I'm guessing this is because the relations are set to inverse in my conventions. I needed to make the relationships inverse() or the cascades didn't work. Here are my conventions:
public class HasManyConvention : IHasManyConvention
{
public void Apply(FluentNHibernate.Conventions.Instances.IOneToManyCollectionInstance instance)
{
instance.Key.Column(instance.EntityType.Name + "ID");
//instance.Inverse();
if (instance.Member.GetCustomAttributes(typeof(CascadeAttribute), false).Length <= 0)
{
instance.Cascade.None();
return;
}
var cascadeOption = (CascadeAttribute)instance.Member.GetCustomAttributes(typeof(CascadeAttribute), false)[0];
switch (cascadeOption.CascadeOption)
{
case Enums.CascadeOptions.All:
instance.Cascade.All();
break;
case Enums.CascadeOptions.AllDeleteOrphan:
instance.Cascade.AllDeleteOrphan();
break;
case Enums.CascadeOptions.Delete:
instance.Cascade.All();
break;
case Enums.CascadeOptions.DeleteOrphan:
instance.Cascade.DeleteOrphan();
break;
case Enums.CascadeOptions.None:
instance.Cascade.None();
break;
case Enums.CascadeOptions.SaveUpdate:
instance.Cascade.SaveUpdate();
break;
}
}
}
public class ReferenceConvention : IReferenceConvention
{
public void Apply(FluentNHibernate.Conventions.Instances.IManyToOneInstance instance)
{
instance.Column(instance.Property.Name + "Fk");
if (Attribute.IsDefined(instance.Property.PropertyType.Assembly, typeof(DomainSignatureAttribute)))
instance.UniqueKey("DomainSignature");
else
instance.Index(instance.Property.Name + "Index");
}
}
And my two entities:
public class Member : Entity
{
public Member()
{
Mail = new List<Mail>();
}
[Cascade(Enums.CascadeOptions.All)]
public virtual IList<Mail> Mail
{
get; set;
}
}
public class Mail : Entity
{
public virtual Member Receiver
{
get; set;
}
public virtual Member Sender
{
get; set;
}
public virtual string Subject
{
get;
set;
}
public virtual string Body
{
get; set;
}
}
Why does fluent nhibernate map MemberId when it's obvious that Receiver and Sender are foreign keys to the member table?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因为
Member.Mail
映射为一对多,这会导致 Mail 中存在外键。没有人说过“Member.Mail”是逆关系,也没有人说过外键是如何调用的。因此它创建了一个标准外键。Because
Member.Mail
is mapped as one-to-many which results in a foreign key in Mail. Nobody ever said that 'Member.Mail' is an inverse relation and nobody ever said how the foreign key is called. So it creates a standard foreign key.