• EF – 6.一对一关联


    5.6.6 《一对一关联概述》 

    5.6.7 《一对一关联CRUD演示》 

    在两讲视频中,首先介绍了数据库中一对一关联表的设计规范,接着通过实例介绍了如何合适Entity Framework针对一对一关联的数据实体对象进行增删改操作。

    5.6.6 《一对一关联概述》 

    Image 6

    5.6.7 《一对一关联CRUD演示》  时长:11分53秒 难度:中

    在两讲视频中,首先介绍了数据库中一对一关联表的设计规范,接着通过实例介绍了如何合适Entity Framework针对一对一关联的数据实体对象进行增删改操作。

    创建对象的方法

    public Person CreatePersonWithoutIdentityCard()
            {
                Person person = new Person() { Name = "ane" };
                return person;
            }
    
            public IdentityCard CreateIndentityCard()
            {
                IdentityCard card = new IdentityCard() { IDNumber = 1 };
                return card;
            }
    


    方法一:在内存中创建好主从对象,并且通过导航属性关联,然后SaveChange()

    /// <summary>
            /// 主对象都是全新的,在内存中关联,然后同事写入数据库
            /// 会生成两条SQL命令,
            /// 第一条插入主对象得到ID,然后再使用此ID设置对象,然后插入
            /// </summary>
            [TestMethod]
            public void TestAdd()
            {
                //1.1.创建一个Person对象,引用唯一的IdentityCard,并且插入数据
                Person person = CreatePersonWithoutIdentityCard();
                person.IdentityCard = CreateIndentityCard();
                //追加到DbSet
                context.Person.Add(person);
    
                //1.2.保存向数据库发送2条SQL命令.
                //第一次为插入Person,返回主键.
                //第二次用返回的主键插入IdentityCard.
                int result = context.SaveChanges();
    
                //1.3.共保存2条数据,所以result == 2
                Assert.IsTrue(result == 2);
            }

    方法二:从数据库中装入主对象,new一个从对象,关联上主对象,然后SaveChange()

    /// <summary>
            /// 主对象是“老的”,从对象是“新”的,在内存中关联,然后写入数据库
            /// </summary>
            [TestMethod]
            public void TestAdd2()
            {
                //2.1.创建一个Person对象,引用唯一的IdentityCard,并且插入数据
                Person person = CreatePersonWithoutIdentityCard();
                //追加到DbSet
                context.Person.Add(person);
                int result = context.SaveChanges();
                Assert.IsTrue(result == 1);
    
                
                //2.2.创建一个新的从对象,并关联主对象
                person.IdentityCard = CreateIndentityCard();
                result = context.SaveChanges();
                //共保存1条数据,所以result == 1
                Assert.IsTrue(result == 1);
    
    
                //2.3.重新装入主从对象,现在两个对象应该都不为null
                Person personFromDB = context.Person.Include("IdentityCard")
                    .FirstOrDefault(p => p.PersonID == person.PersonID);
                Assert.IsNotNull(personFromDB);
                Assert.IsNotNull(personFromDB.IdentityCard);
            }

    错误方法

    /// <summary>
            /// 单独创建一个从对象,试图插入数据库,将会报告DbUpdateException异常
            /// 最终数据没有插入,所以,“永远不要在一对一关联中单独插入从对象”
            /// </summary>
            [TestMethod]
            //[ExpectedException(typeof(DbUpdateException))]
            public void TestAdd3()
            {
                //3.1 创建一个“独立的”从对象
                IdentityCard idCard = CreateIndentityCard();
                //获取主键
                int maxId = context.IdentityCard.Max(id => id.IdentityCardId);
                idCard.IdentityCardId = maxId + 1;
                //追加到DbSet
                context.IdentityCard.Add(idCard);
                //由于对应的主记录不存在,所以插入数据失败
                int result = context.SaveChanges();
                //此断言永远不可能被满足
                Assert.IsTrue(result > 0);
            }

    image

    删除对象:

    image

    修改对象:

    image

  • 相关阅读:
    Delegates in C#
    Continues Integration
    单例模式(Singleton Pattern)
    敏捷开发中编写高质量Java代码
    How to debug your application (http protocol) using Fiddler
    Java EE核心框架实战(1)
    03 Mybatis:01.Mybatis课程介绍及环境搭建&&02.Mybatis入门案例
    04 Spring:01.Spring框架简介&&02.程序间耦合&&03.Spring的 IOC 和 DI&&08.面向切面编程 AOP&&10.Spring中事务控制
    黑马IDEA版javaweb_22MySQL
    第04项目:淘淘商城(SpringMVC+Spring+Mybatis) 的学习实践总结【第二天】
  • 原文地址:https://www.cnblogs.com/tangge/p/4535922.html
Copyright © 2020-2023  润新知