Fluent-nhibernate 自动映射外键插入 null。
我有一个名为 Worker 的类,
public class Worker : BaseEntity
{
public virtual int WorkerID { get; set; }
public virtual string Name { get; set; }
public virtual IList<Indemnification> Indemnifications { get; set; }
}
public class Indemnification : BaseEntity
{
public virtual int IndemnificationID { get; set; }
public virtual string IndemnificationNumber { get; set; }
//another properties
}
我正在使用带有一些约定的自动映射
var mappings = new AutoPersistenceModel();
mappings.AddEntityAssembly(typeof(Worker).Assembly).Where(GetAutoMappingFilter);
mappings.Conventions.Setup(GetConventions());
mappings.Setup(GetSetup());
private Action<IConventionFinder> GetConventions()
{
return c =>
{
c.Add<PrimaryKeyConvention>();
c.Add<HasManyConvention>();
c.Add<TableNameConvention>();
c.Add<CustomForeignKeyConvention>();
c.Add<SubClassConvention>();
};
}
public class PrimaryKeyConvention : IIdConvention
{
public void Apply(FluentNHibernate.Conventions.Instances.IIdentityInstance instance)
{
instance.Column(instance.EntityType.Name + "ID");
instance.UnsavedValue("0");
}
}
public class HasManyConvention : IHasManyConvention
{
public void Apply(FluentNHibernate.Conventions.Instances.IOneToManyCollectionInstance instance)
{
instance.Key.Column(instance.EntityType.Name + "ID");
instance.Cascade.AllDeleteOrphan();
}
}
public class TableNameConvention : IClassConvention
{
public void Apply(FluentNHibernate.Conventions.Instances.IClassInstance instance)
{
instance.Table(instance.EntityType.Name);
}
}
public class CustomForeignKeyConvention : ForeignKeyConvention
{
protected override string GetKeyName(Member property, Type type)
{
return type.Name + "ID";
}
}
public class SubClassConvention : IJoinedSubclassConvention
{
public void Apply(FluentNHibernate.Conventions.Instances.IJoinedSubclassInstance instance)
{
instance.Table(instance.EntityType.Name);
instance.Key.Column(instance.EntityType.BaseType.Name + "ID");
}
}
问题是当我使用赔偿列表保存 Worker 时: 工人被保存,因此除了外键(WorkerID)之外的赔偿 赔偿表为空???
I have a class called Worker
public class Worker : BaseEntity
{
public virtual int WorkerID { get; set; }
public virtual string Name { get; set; }
public virtual IList<Indemnification> Indemnifications { get; set; }
}
public class Indemnification : BaseEntity
{
public virtual int IndemnificationID { get; set; }
public virtual string IndemnificationNumber { get; set; }
//another properties
}
i am using automapping with some conventions
var mappings = new AutoPersistenceModel();
mappings.AddEntityAssembly(typeof(Worker).Assembly).Where(GetAutoMappingFilter);
mappings.Conventions.Setup(GetConventions());
mappings.Setup(GetSetup());
private Action<IConventionFinder> GetConventions()
{
return c =>
{
c.Add<PrimaryKeyConvention>();
c.Add<HasManyConvention>();
c.Add<TableNameConvention>();
c.Add<CustomForeignKeyConvention>();
c.Add<SubClassConvention>();
};
}
public class PrimaryKeyConvention : IIdConvention
{
public void Apply(FluentNHibernate.Conventions.Instances.IIdentityInstance instance)
{
instance.Column(instance.EntityType.Name + "ID");
instance.UnsavedValue("0");
}
}
public class HasManyConvention : IHasManyConvention
{
public void Apply(FluentNHibernate.Conventions.Instances.IOneToManyCollectionInstance instance)
{
instance.Key.Column(instance.EntityType.Name + "ID");
instance.Cascade.AllDeleteOrphan();
}
}
public class TableNameConvention : IClassConvention
{
public void Apply(FluentNHibernate.Conventions.Instances.IClassInstance instance)
{
instance.Table(instance.EntityType.Name);
}
}
public class CustomForeignKeyConvention : ForeignKeyConvention
{
protected override string GetKeyName(Member property, Type type)
{
return type.Name + "ID";
}
}
public class SubClassConvention : IJoinedSubclassConvention
{
public void Apply(FluentNHibernate.Conventions.Instances.IJoinedSubclassInstance instance)
{
instance.Table(instance.EntityType.Name);
instance.Key.Column(instance.EntityType.BaseType.Name + "ID");
}
}
the problem is when i save Worker with a list of Indemnifications:
the worker is saved, and so the Indemnifications but the foreign key (WorkerID) in
the Indemnification table is null????
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我解决了问题:
当您需要保存具有(一对多)关系的实体时,您需要打开一个事务并提交它:)。
I figured out the problem:
when you need to save an entity which has (one to many) relationship, you need to open a transaction and commit it :).
您是否想知道为什么自动映射首先允许为一对多关系创建的外键为空?
那么在您的示例中,为什么“赔偿”表中的“workerId”列没有添加非空约束?
我刚刚遇到这个问题,我认为即使可以在代码中处理它,但根本不可能插入空值,对吧?有什么解决办法吗?
Didn´t you wonder why the automapping allowed foreign keys that are created for a one-to-many relation ship to be null in the first place?
So in your example why does the column "workerId" in the table "Indemnification" not have the not null constraint added to it?
I just came across the the problem and I think even though it can be handled in code, it should not be possible at all to insert a null value, right? Any solution for that?