微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在单向一对多关系中重新父母

如何解决如何在单向一对多关系中重新父母

如何在单向一对多关系中重新父级?我本质上是想让 ParentA 的集合中的 1 个孩子移动到 ParentB 的集合中。

映射

        public class Person_10
        {
            public virtual int Id { get; set; }
            public virtual string FirstName { get; set; }
            public virtual IList<Dog_10> Dogs { get; set; }

            public Person_10()
            {

            }

        }
        
        public class Person_10Map : ClassMap<Person_10>
        {
            public Person_10Map()
            {
                Id(x => x.Id).GeneratedBy.Identity();

                Map(x => x.FirstName).Not.Nullable();
                // Have to set this or the lineitems wont get automatically persisted when the PersistenceSpecification
                // saves the order!
                HasMany(x => x.Dogs)
                    .Not
                    .KeyNullable() // important and work only if set together ( if i don't have this it wont update the foreign key column... for this unidirectional relationship..
                    .Not
                    .KeyUpdate() // to prevent double update.  Without this you get insert(cart),insert(lineitem),update ( lineitem )
                    .Cascade.AllDeleteOrphan();
            }
        }

        public class Dog_10
        {
            public virtual int Id { get; set; }
            public virtual string Name { get; set; }

        }
        

        public class Dog_10Map : ClassMap<Dog_10>
        {
            public Dog_10Map()
            {
                Id(x => x.Id).GeneratedBy.Identity();

                Map(x => x.Name).Not.Nullable();

            }
        }

示例代码

        [Test]
        public void UniDirectionalreparenting()
        {
            using (var session = AppSessionFactory.OpenSession())
            using (var transaction = session.BeginTransaction())
            {
                var parentA = new Person_10
                {
                    FirstName = "Michael",Dogs = new List<Dog_10>
                    {
                        new Dog_10
                        {
                            Name = "Sheba"
                        },new Dog_10
                        {
                            Name = "Hercules"
                        }
                    }
                };
                session.Save(parentA);
                transaction.Commit();
            }

            using (var session = AppSessionFactory.OpenSession())
            using (var transaction = session.BeginTransaction())
            {
                // Query on the computed persisted property!
                var parentA = session.Query<Person_10>().FirstOrDefault(x => x.FirstName == "Michael");
                var parentB = new Person_10
                {
                    FirstName = "Jennifer",Dogs = new List<Dog_10>()
                };
                foreach (var dog in parentA.Dogs)
                {
                    parentB.Dogs.Add(dog);
                }
 
                
                session.Save(parentB);
                transaction.Commit();
            }
        }

现在,父 A 在第一笔交易中与所有子项正确创建,如下所示

-- statement #1
INSERT INTO dbo. [Person_10]
            (FirstName)
VALUES      ('Michael' /* @p0 - FirstName */);

--//////////////////////////////////////////////////

select ScopE_IDENTITY()


-- statement #2
INSERT INTO dbo. [Dog_10]
            (Name,Person_10_id)
VALUES      ('Sheba' /* @p0 - Name */,1 /* @p1 - Person_10_id */);

--//////////////////////////////////////////////////

select ScopE_IDENTITY()


-- statement #3
INSERT INTO dbo. [Dog_10]
            (Name,Person_10_id)
VALUES      ('Hercules' /* @p0 - Name */,1 /* @p1 - Person_10_id */);

--//////////////////////////////////////////////////

select ScopE_IDENTITY()

但是在第二个事务中,当尝试将子级移动到另一个父级时,它没有执行我期望的更新语句,如下面的 sql

-- statement #1
select TOP (1) reparentin0_.Id        as id1_21_,reparentin0_.FirstName as firstname2_21_
from   dbo.[Person_10] reparentin0_
where  reparentin0_.FirstName = 'Michael' /* @p0 */

-- statement #2
SELECT dogs0_.Person_10_id as person3_19_1_,dogs0_.Id           as id1_19_1_,dogs0_.Id           as id1_19_0_,dogs0_.Name         as name2_19_0_
FROM   dbo.[Dog_10] dogs0_
WHERE  dogs0_.Person_10_id = 1 /* @p0 */

-- statement #3
INSERT INTO dbo. [Person_10]
            (FirstName)
VALUES      ('Jennifer' /* @p0 - FirstName */);

--//////////////////////////////////////////////////

select ScopE_IDENTITY()


                
                
            

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。