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

发布于 2024-09-18 09:32:34 字数 2534 浏览 8 评论 0原文

我有一个名为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烙印 2024-09-25 09:32:34

我解决了问题:

当您需要保存具有(一对多)关系的实体时,您需要打开一个事务并提交它:)。

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

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();
清泪尽 2024-09-25 09:32:34

您是否想知道为什么自动映射首先允许为一对多关系创建的外键为空?

那么在您的示例中,为什么“赔偿”表中的“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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文