广告
返回顶部
首页 > 资讯 > 后端开发 > ASP.NET >Entity Framework管理一对一实体关系
  • 894
分享到

Entity Framework管理一对一实体关系

2024-04-02 19:04:59 894人浏览 薄情痞子
摘要

我们现在已经知道如何使用Code First来定义简单的领域类,并且如何使用DbContext类来执行数据库操作。现在我们来看下数据库理论中的多样性关系,我们会使用Code Firs

我们现在已经知道如何使用Code First来定义简单的领域类,并且如何使用DbContext类来执行数据库操作。现在我们来看下数据库理论中的多样性关系,我们会使用Code First来实现下面的几种关系:

  • 1、一对一关系: one to one
  • 2、一对多关系: one to many
  • 3、多对多关系::many to many

首先要明确关系的概念。关系就是定义两个或多个对象之间是如何关联的。它是由关系两端的多样性值识别的,比如,一对多意味着在关系的一端,只有一个实体,我们有时称为父母;在关系的另一端,可能有多个实体,有时称为孩子。EF api将那些端分别称为主体和依赖。一对多关系也叫做一或零对多(One-or-Zero-to-Many),这意味着一个孩子可能有或可能没有父母。一对一关系也稍微有些变化,就是关系的两端都是可选的。

一、EF里的实体关系配置

Has方法

With方法

配置实体关系:

一对一表关系设计:

一对一关系并不常用,但是偶尔也会出现。如果一个实体有一些可选的数据,那么你可以选择这种设计。

二、使用数据注解配置一对一关系

示例中Person表作为主表,IDCard表作为从表。

1、新建实体类

Person实体类结构如下:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 配置一对一实体关系.Model
{
    /// <summary>
    /// 主表
    /// </summary>
    [Table("Person")]
    public class Person
    {
        [Key]
        public int PersonId { get; set; }

        public string Name { get; set; }

        public int Sex { get; set; }

        public int Age { get; set; }

        /// <summary>
        /// virtual 表示是导航属性 启用贪懒加载
        /// </summary>
        [ForeignKey("PersonId")]
        public virtual IDCard IDCard { get; set; }
    }
}

IDCard实体类结构如下:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 配置一对一实体关系.Model
{
    public class IDCard
    {
        [Key]
        public int PersonId { get; set; }
        public string IDCardNo { get; set; }

        public DateTime Dataissue { get; set; }

        public DateTime ValidTime { get; set; }

        public string IssuingAuthority { get; set; }

        /// <summary>
        /// 导航属性
        /// </summary>
        public virtual Person Person { get; set; }
    }
}

2、创建EF数据上下文类

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using 配置一对一实体关系.Model;

namespace 配置一对一实体关系.EF
{
    public class EFDbContext :DbContext
    {
        public EFDbContext()
            : base("name=CodeFirstApplication")
        {

        }

        public DbSet<Person> Persons { get; set; }

        public DbSet<IDCard> IDCards { get; set; }
    }
}

3、使用数据迁移的方式创建数据库

在使用数据迁移的过程中报错:Unable to determine the principal end of an association between the types '配置一对一实体关系.Model.Person' and '配置一对一实体关系.Model.IDCard'. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations。通过查找资料,解决办法如下:添加Required数据注解,修改后的Person类结构如下:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 配置一对一实体关系.Model
{
    /// <summary>
    /// 主表
    /// </summary>
    [Table("Person")]
    public class Person
    {
        [Key]
        public int PersonId { get; set; }

        public string Name { get; set; }

        public int Sex { get; set; }

        public int Age { get; set; }

        /// <summary>
        /// virtual 表示是导航属性 启用贪懒加载
        /// </summary>
        [ForeignKey("PersonId")]
        [Required]
        public virtual IDCard IDCard { get; set; }
    }
}

4、查看生成的数据库表结构

通过查看数据库表结构,发现Person表和IDCards表建立了主外键关系。

总结:使用数据注解配置一对一关系的步骤:

1、使用数据注解Key标识主键。
2、两个实体之间的主键Key必须相同。
3、两个实体之间有相互引用的导航属性(使用virtual)。
4、在主表中设置外键关系[ForeignKey("PersonId")]。

三、使用Fluent API来配置一对一关系

1、创建A实体类结构如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 使用FluentAPI实现.Model
{
    public class A
    {
        public int AId { get; set; }
        public string Name { get; set; }
        public virtual B B { get; set; }
    }
}

2、创建B实体类结构如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 使用FluentAPI实现.Model
{
    public class B
    {
        public int AId { get; set; }
        public string Name { get; set; }
        public virtual A A { get; set; }
    }
}

3、使用Fluent API实现

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using 使用FluentAPI实现.Model;

namespace 使用FluentAPI实现.EF
{
    public class EFDbContext :DbContext
    {
        public EFDbContext()
            : base("name=CodeFirstApplication")
        { }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<IDCard>().ToTable("IDCard").HasKey(p => p.PersonId);
            modelBuilder.Entity<Person>().ToTable("Person").HasKey(p => p.PersonId);


            #region 1.0 默认一对一配置
            //modelBuilder.Entity<Person>().HasRequired(p => p.IDCard).WithOptional();

            #endregion

            #region 2.0 指定谁是主体对象谁是依赖对象
            // 指定当前Person对象依赖IDCard对象,外键会创建到IDCard对象中,而IDCard对象是独立存在的表,这种依赖关系配置是错误的。
            //modelBuilder.Entity<Person>().HasRequired(p => p.IDCard).WithRequiredDependent(t => t.Person);
            // 正确的依赖配置如下:
            //modelBuilder.Entity<IDCard>().HasRequired(p => p.Person).WithRequiredDependent(t => t.IDCard);
            //modelBuilder.Entity<A>().HasRequired(p => p.B).WithRequiredDependent(d=>d.A);//WithRequiredDependent A依赖对象(A依赖B,B可以对立存在,A会被建立外键)

            #endregion   指定谁是主要的对象
            modelBuilder.Entity<IDCard>().HasRequired(p => p.Person).WithRequiredPrincipal(t => t.IDCard);
            //WithRequiredPrincipal A 主体对象,执行A对象为被继承者,也就是父级,B继承A,A独立存在
            //modelBuilder.Entity<A>().HasRequired(p => p.B).WithRequiredPrincipal(d => d.A);
            #region MyRegion

            #endregion
            base.OnModelCreating(modelBuilder);
        }
    }
}

这里使用了HasKey方法,指定了一个表的主键,换言之,这是一个允许我们找到一个实体的独一无二的值。之前我们没有用这个方法是因为我们要么用了Key特性或者遵守了EF的默认约定(如果属性名是由类名加上"Id"后缀或者只是"Id"组成,那么EF会计算出该主键)。因为我们现在使用了PersonId作为主键,所以我们现在需要给运行时提供额外的提示,这就是HasKey派生用场的地方。最后子表中的主键会成为父表中的外键。

因为该关系是可选的,所以它也称为一或零对一(One-or-Zero-to-One)。关系的两端都是必须要存在的关系称为一对一。比如,每个人必须要有一个单独的login,这是强制性的。你也可以使用WithRequiredDepentent或者WithRequiredPrincipal方法来代替WithOptional方法。

注意:我们可以总是从该关系的主体端或者依赖端来配置关系。我们总是需要配置一对一关系的两端(即两个实体),使用Has和With方法确保一对一关系的创建。

到此这篇关于Entity Framework管理一对一实体关系的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持编程网。

--结束END--

本文标题: Entity Framework管理一对一实体关系

本文链接: https://www.lsjlt.com/news/141396.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作