最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Entity?Framework管理一對一實(shí)體關(guān)系

 更新時(shí)間:2022年03月05日 09:35:12   作者:.NET開發(fā)菜鳥  
本文詳細(xì)講解了Entity?Framework管理一對一實(shí)體關(guān)系的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

我們現(xiàn)在已經(jīng)知道如何使用Code First來定義簡單的領(lǐng)域類,并且如何使用DbContext類來執(zhí)行數(shù)據(jù)庫操作?,F(xiàn)在我們來看下數(shù)據(jù)庫理論中的多樣性關(guān)系,我們會(huì)使用Code First來實(shí)現(xiàn)下面的幾種關(guān)系:

  • 1、一對一關(guān)系: one to one
  • 2、一對多關(guān)系: one to many
  • 3、多對多關(guān)系::many to many

首先要明確關(guān)系的概念。關(guān)系就是定義兩個(gè)或多個(gè)對象之間是如何關(guān)聯(lián)的。它是由關(guān)系兩端的多樣性值識(shí)別的,比如,一對多意味著在關(guān)系的一端,只有一個(gè)實(shí)體,我們有時(shí)稱為父母;在關(guān)系的另一端,可能有多個(gè)實(shí)體,有時(shí)稱為孩子。EF API將那些端分別稱為主體和依賴。一對多關(guān)系也叫做一或零對多(One-or-Zero-to-Many),這意味著一個(gè)孩子可能有或可能沒有父母。一對一關(guān)系也稍微有些變化,就是關(guān)系的兩端都是可選的。

一、EF里的實(shí)體關(guān)系配置

Has方法

With方法

配置實(shí)體關(guān)系:

一對一表關(guān)系設(shè)計(jì):

一對一關(guān)系并不常用,但是偶爾也會(huì)出現(xiàn)。如果一個(gè)實(shí)體有一些可選的數(shù)據(jù),那么你可以選擇這種設(shè)計(jì)。

二、使用數(shù)據(jù)注解配置一對一關(guān)系

示例中Person表作為主表,IDCard表作為從表。

1、新建實(shí)體類

Person實(shí)體類結(jié)構(gòu)如下:

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 配置一對一實(shí)體關(guān)系.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 表示是導(dǎo)航屬性 啟用貪懶加載
        /// </summary>
        [ForeignKey("PersonId")]
        public virtual IDCard IDCard { get; set; }
    }
}

IDCard實(shí)體類結(jié)構(gòu)如下:

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

namespace 配置一對一實(shí)體關(guān)系.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>
        /// 導(dǎo)航屬性
        /// </summary>
        public virtual Person Person { get; set; }
    }
}

2、創(chuàng)建EF數(shù)據(jù)上下文類

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using 配置一對一實(shí)體關(guān)系.Model;

namespace 配置一對一實(shí)體關(guān)系.EF
{
    public class EFDbContext :DbContext
    {
        public EFDbContext()
            : base("name=CodeFirstApplication")
        {

        }

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

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

3、使用數(shù)據(jù)遷移的方式創(chuàng)建數(shù)據(jù)庫

在使用數(shù)據(jù)遷移的過程中報(bào)錯(cuò):Unable to determine the principal end of an association between the types '配置一對一實(shí)體關(guān)系.Model.Person' and '配置一對一實(shí)體關(guān)系.Model.IDCard'. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations。通過查找資料,解決辦法如下:添加Required數(shù)據(jù)注解,修改后的Person類結(jié)構(gòu)如下:

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 配置一對一實(shí)體關(guān)系.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 表示是導(dǎo)航屬性 啟用貪懶加載
        /// </summary>
        [ForeignKey("PersonId")]
        [Required]
        public virtual IDCard IDCard { get; set; }
    }
}

4、查看生成的數(shù)據(jù)庫表結(jié)構(gòu)

通過查看數(shù)據(jù)庫表結(jié)構(gòu),發(fā)現(xiàn)Person表和IDCards表建立了主外鍵關(guān)系。

總結(jié):使用數(shù)據(jù)注解配置一對一關(guān)系的步驟:

1、使用數(shù)據(jù)注解Key標(biāo)識(shí)主鍵。
2、兩個(gè)實(shí)體之間的主鍵Key必須相同。
3、兩個(gè)實(shí)體之間有相互引用的導(dǎo)航屬性(使用virtual)。
4、在主表中設(shè)置外鍵關(guān)系[ForeignKey("PersonId")]。

三、使用Fluent API來配置一對一關(guān)系

1、創(chuàng)建A實(shí)體類結(jié)構(gòu)如下:

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

namespace 使用FluentAPI實(shí)現(xiàn).Model
{
    public class A
    {
        public int AId { get; set; }
        public string Name { get; set; }
        public virtual B B { get; set; }
    }
}

2、創(chuàng)建B實(shí)體類結(jié)構(gòu)如下:

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

namespace 使用FluentAPI實(shí)現(xiàn).Model
{
    public class B
    {
        public int AId { get; set; }
        public string Name { get; set; }
        public virtual A A { get; set; }
    }
}

3、使用Fluent API實(shí)現(xiàn)

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using 使用FluentAPI實(shí)現(xiàn).Model;

namespace 使用FluentAPI實(shí)現(xiàn).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 默認(rèn)一對一配置
            //modelBuilder.Entity<Person>().HasRequired(p => p.IDCard).WithOptional();

            #endregion

            #region 2.0 指定誰是主體對象誰是依賴對象
            // 指定當(dāng)前Person對象依賴IDCard對象,外鍵會(huì)創(chuàng)建到IDCard對象中,而IDCard對象是獨(dú)立存在的表,這種依賴關(guān)系配置是錯(cuò)誤的。
            //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會(huì)被建立外鍵)

            #endregion   指定誰是主要的對象
            modelBuilder.Entity<IDCard>().HasRequired(p => p.Person).WithRequiredPrincipal(t => t.IDCard);
            //WithRequiredPrincipal A 主體對象,執(zhí)行A對象為被繼承者,也就是父級(jí),B繼承A,A獨(dú)立存在
            //modelBuilder.Entity<A>().HasRequired(p => p.B).WithRequiredPrincipal(d => d.A);
            #region MyRegion

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

這里使用了HasKey方法,指定了一個(gè)表的主鍵,換言之,這是一個(gè)允許我們找到一個(gè)實(shí)體的獨(dú)一無二的值。之前我們沒有用這個(gè)方法是因?yàn)槲覀円从昧薑ey特性或者遵守了EF的默認(rèn)約定(如果屬性名是由類名加上"Id"后綴或者只是"Id"組成,那么EF會(huì)計(jì)算出該主鍵)。因?yàn)槲覀儸F(xiàn)在使用了PersonId作為主鍵,所以我們現(xiàn)在需要給運(yùn)行時(shí)提供額外的提示,這就是HasKey派生用場的地方。最后子表中的主鍵會(huì)成為父表中的外鍵。

因?yàn)樵撽P(guān)系是可選的,所以它也稱為一或零對一(One-or-Zero-to-One)。關(guān)系的兩端都是必須要存在的關(guān)系稱為一對一。比如,每個(gè)人必須要有一個(gè)單獨(dú)的login,這是強(qiáng)制性的。你也可以使用WithRequiredDepentent或者WithRequiredPrincipal方法來代替WithOptional方法。

注意:我們可以總是從該關(guān)系的主體端或者依賴端來配置關(guān)系。我們總是需要配置一對一關(guān)系的兩端(即兩個(gè)實(shí)體),使用Has和With方法確保一對一關(guān)系的創(chuàng)建。

到此這篇關(guān)于Entity Framework管理一對一實(shí)體關(guān)系的文章就介紹到這了。希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • .net中的DI框架AutoFac簡單介紹

    .net中的DI框架AutoFac簡單介紹

    這篇文章介紹了.net中的DI框架AutoFac,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07
  • ASP.NET―001:GridView綁定List、頁面返回值具體實(shí)現(xiàn)

    ASP.NET―001:GridView綁定List、頁面返回值具體實(shí)現(xiàn)

    這篇文章主要介紹了ASP.NET―GridView綁定List、頁面返回值具體實(shí)現(xiàn),需要的朋友可以參考下
    2014-02-02
  • Asp.net MVC中Razor常見的問題與解決方法總結(jié)

    Asp.net MVC中Razor常見的問題與解決方法總結(jié)

    這篇文章主要給大家介紹了關(guān)于Asp.net MVC中Razor常見的問題與解決方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面跟著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-08-08
  • ASP.NET MVC分頁問題解決

    ASP.NET MVC分頁問題解決

    這篇文章主要為大家詳細(xì)介紹了ASP.NET MVC分頁問題的解決方法,Ajax.Pager分頁的使用注意事項(xiàng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-01-01
  • asp.net中for和do循環(huán)語句用法分享

    asp.net中for和do循環(huán)語句用法分享

    文章介紹了兩個(gè)實(shí)例一個(gè)是FOR循環(huán)創(chuàng)建一個(gè)Mandelbrot圖像,循環(huán)結(jié)構(gòu)之DO語句,根據(jù)布爾值的測試結(jié)果,執(zhí)行相應(yīng)代碼,有需要的朋友可參考一下
    2012-04-04
  • 用WebClient.UploadData方法上載文件數(shù)據(jù)的方法

    用WebClient.UploadData方法上載文件數(shù)據(jù)的方法

    用WebClient.UploadData方法上載文件數(shù)據(jù)的方法...
    2007-04-04
  • Asp.NET頁面中事件加載的先后順序詳解

    Asp.NET頁面中事件加載的先后順序詳解

    這篇文章主要給大家介紹了關(guān)于Asp.NET頁面中事件加載的先后順序,文中通過圖文以及示例代碼介紹的非常詳細(xì),對大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-08-08
  • ASP.NET實(shí)現(xiàn)根據(jù)IP獲取省市地址的方法

    ASP.NET實(shí)現(xiàn)根據(jù)IP獲取省市地址的方法

    這篇文章主要介紹了ASP.NET實(shí)現(xiàn)根據(jù)IP獲取省市地址的方法,主要基于QQwry.dat純真IP數(shù)據(jù)庫來實(shí)現(xiàn)這一功能,非常實(shí)用,需要的朋友可以參考下
    2014-10-10
  • asp.net實(shí)現(xiàn)DropDownList,TreeView,ListBox的無限極分類目錄樹

    asp.net實(shí)現(xiàn)DropDownList,TreeView,ListBox的無限極分類目錄樹

    這篇文章主要介紹了asp.net實(shí)現(xiàn)DropDownList,TreeView,ListBox的無限極分類目錄樹,結(jié)合實(shí)例形式較為詳細(xì)的分析了asp.net常見控件實(shí)現(xiàn)無限極分類目錄樹的具體實(shí)現(xiàn)步驟與相關(guān)操作技巧,需要的朋友可以參考下
    2016-06-06
  • ASP.NET的HtmlForm控件學(xué)習(xí)及Post與Get的區(qū)別概述

    ASP.NET的HtmlForm控件學(xué)習(xí)及Post與Get的區(qū)別概述

    HtmlForm 控件用于控制form元素,本文主要介紹下HtmlForm控件的Method/Action方法(要提交數(shù)據(jù)的頁面,即數(shù)據(jù)要傳送至哪個(gè)網(wǎng)址)及Post與Get的區(qū)別感興趣的朋友可以了解下,或許對你學(xué)習(xí)HtmlForm控件有所幫助
    2013-02-02

最新評論

疏附县| 富顺县| 封丘县| 阿城市| 富锦市| 阿尔山市| 颍上县| 麦盖提县| 望江县| 健康| 泸水县| 全州县| 香河县| 石嘴山市| 杭锦后旗| 彭阳县| 平顺县| 望城县| 义乌市| 华亭县| 姜堰市| 肇庆市| 盐津县| 林西县| 铜陵市| 平南县| 永寿县| 晴隆县| 花垣县| 会宁县| 葫芦岛市| 宁津县| 岳阳县| 乌兰察布市| 沁源县| 金堂县| 东乡族自治县| 水城县| 曲阳县| 海城市| 太仓市|