简体   繁体   English

fluent-nhibernate自动映射外键将插入null。

[英]fluent-nhibernate automapping foreign key inserts null.

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???? 问题是当我用赔偿列表保存Worker时:工作者已保存,因此赔偿但Indemnification表中的外键(WorkerID)为空?

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 :). 当您需要保存具有(一对多)关系的实体时,您需要打开一个事务并提交它:)。

Session.BeginTransaction();
Session.Save(entity);
Session.CommitTransaction();

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? 因此,在您的示例中,为什么表“ Indemnification”中的“ workerId”列没有添加not null约束?

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? 有什么解决办法吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用fluent-nhibernate自动化在外键上添加多列唯一约束 - Add multi-column unique constraint on foreign Key using fluent-nhibernate automapping 流利的 nhibernate 自动映射一对多使外键 null - fluent nhibernate automapping one to many makes foreign key null 使用fluent-nhibernate,是否存在使一对多关系中的外键列不为空的约定? - Using fluent-nhibernate, is there convention to make the foreign-key column in one-to-many relation not null? 流利的NHibernate HasMany映射在外键中插入null - Fluent NHibernate HasMany mapping inserts null in foreign key NHibernate流利的HasMany映射插入NULL外键 - NHibernate fluent HasMany mapping inserts NULL Foreign key fluent-nhibernate HasOne关系返回NULL - fluent-nhibernate HasOne relationship returning NULL 没有主键的Fluent-NHibernate表映射 - Fluent-NHibernate table mapping with no primary key 流利的Nhibernate AutoMapping - 同桌的2个外键? - Fluent Nhibernate AutoMapping — 2 foreign keys to same table? Fluent-Nhibernate持久性规范测试生成SQLite错误外键不匹配 - Fluent-Nhibernate Persistence Specification test generating SQLite error foreign key mismatch 非空外键的流畅 nhibernate 映射(HasMany) - fluent nhibernate mapping with not null foreign key (HasMany)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM