日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無(wú)先后,達(dá)者為師

網(wǎng)站首頁(yè) 編程語(yǔ)言 正文

ASP.NET?Core使用EF為關(guān)系數(shù)據(jù)庫(kù)建模_實(shí)用技巧

作者:暗斷腸 ? 更新時(shí)間: 2022-06-09 編程語(yǔ)言

1.簡(jiǎn)介

一般而言,本部分中的配置適用于關(guān)系數(shù)據(jù)庫(kù)。安裝關(guān)系數(shù)據(jù)庫(kù)提供程序時(shí),此處顯示的變?yōu)榭捎脭U(kuò)展方法(原因在于共享的Microsoft.EntityFrameworkCore.Relational包)。

2.表映射

表映射標(biāo)識(shí)在數(shù)據(jù)庫(kù)中哪張表應(yīng)該進(jìn)行內(nèi)容查詢和保存操作。

2.1約定

按照約定,每個(gè)實(shí)體將設(shè)置為映射到名稱(chēng)與DbSet 屬性(公開(kāi)派生上下文中的實(shí)體)相同的表中。如果給定DbSet實(shí)體中不包含,則使用類(lèi)名稱(chēng)。

2.2數(shù)據(jù)注釋

可以使用數(shù)據(jù)注釋來(lái)配置類(lèi)型映射表。

[Table("blogs")]
public class Blog
{
    public int BlogId { get; set; }
    public string Url { get; set; }
}

你還可以指定表所屬的架構(gòu)(數(shù)據(jù)庫(kù))。

[Table("blogs", Schema = "blogging")]
public class Blog
{
    public int BlogId { get; set; }
    public string Url { get; set; }
}

2.3Fluent API

你可以使用熟知的API來(lái)配置類(lèi)型映射到的表。

class MyContext : DbContext
{
    public DbSet Blogs { get; set; }
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity()
            .ToTable("blogs");
    }
}
public class Blog
{
    public int BlogId { get; set; }
    public string Url { get; set; }
}

你還可以指定表所屬的架構(gòu)(數(shù)據(jù)庫(kù))。

modelBuilder.Entity().ToTable("blogs", schema: "blogging");

3.列映射

列映射標(biāo)識(shí)在數(shù)據(jù)庫(kù)中應(yīng)從哪些列數(shù)據(jù)中進(jìn)行查詢和保存。

3.1約定

按照約定,每個(gè)屬性將會(huì)設(shè)置為映射到與屬性具有相同名稱(chēng)的列。

3.2數(shù)據(jù)注釋

可以使用數(shù)據(jù)注釋來(lái)配置屬性映射到的那一列。

namespace EFModeling.DataAnnotations.Relational.Column
{
    class MyContext : DbContext
    {
        public DbSet Blogs { get; set; }
    }
    public class Blog
    {
        [Column("blog_id")]
        public int BlogId { get; set; }
        public string Url { get; set; }
    }
}

3.3Fluent API

您可以使用熟知的API來(lái)配置屬性映射到的列。

namespace EFModeling.FluentAPI.Relational.Column
{
    class MyContext : DbContext
    {
        public DbSet Blogs { get; set; }
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity()
                .Property(b => b.BlogId)
                .HasColumnName("blog_id");
        }
    }
    public class Blog
    {
        public int BlogId { get; set; }
        public string Url { get; set; }
    }
}

4.數(shù)據(jù)類(lèi)型

數(shù)據(jù)類(lèi)型是指屬性所映射到的列的數(shù)據(jù)庫(kù)特定類(lèi)型。

4.1約定

按照約定,數(shù)據(jù)庫(kù)提供程序基于屬性的.NET類(lèi)型選擇數(shù)據(jù)類(lèi)型。它還會(huì)考慮其他元數(shù)據(jù),如配置的最大長(zhǎng)度、屬性是否是主鍵的一部分等。例如,SQL Server的DateTime、nvarchar(max) 用作鍵的屬性。

4.2數(shù)據(jù)注釋

您可以使用數(shù)據(jù)注釋為列指定精確的數(shù)據(jù)類(lèi)型。例如,下面的代碼將Url配置為一個(gè)非unicode字符串,其最大200長(zhǎng)度。Rating為5至2小數(shù)位。

public class Blog
{
    public int BlogId { get; set; }
    [Column(TypeName = "varchar(200)")]
    public string Url { get; set; }
    [Column(TypeName = "decimal(5, 2)")]
    public decimal Rating { get; set; }
}

4.3Fluent API

你還可以使用熟知的API為列指定相同的數(shù)據(jù)類(lèi)型。

class MyContext : DbContext
{
    public DbSet Blogs { get; set; }
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity(eb =>
        {
            eb.Property(b => b.Url).HasColumnType("varchar(200)");
            eb.Property(b => b.Rating).HasColumnType("decimal(5, 2)");
        });
    }
}
public class Blog
{
    public int BlogId { get; set; }
    public string Url { get; set; }
    public decimal Rating { get; set; }
}

5.主鍵

為每個(gè)實(shí)體類(lèi)型的鍵引入primary key(主鍵)約束。

5.1約定

按照約定,會(huì)將數(shù)據(jù)庫(kù)中的主鍵命名為PK_

5.2數(shù)據(jù)注釋

不能使用數(shù)據(jù)批注配置主鍵的關(guān)系數(shù)據(jù)庫(kù)的特定方面。

5.3Fluent API

你可以使用API在數(shù)據(jù)庫(kù)中配置primary key(主鍵)約束的名稱(chēng)。

class MyContext : DbContext
{
    public DbSet Blogs { get; set; }
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity()
            .HasKey(b => b.BlogId)
            .HasName("PrimaryKey_BlogId");
    }
}
public class Blog
{
    public int BlogId { get; set; }
    public string Url { get; set; }
}

6.默認(rèn)架構(gòu)

如果沒(méi)有為該對(duì)象顯式配置架構(gòu),則默認(rèn)架構(gòu)為將在其中創(chuàng)建對(duì)象的數(shù)據(jù)庫(kù)架構(gòu)。

6.1約定

按照約定,數(shù)據(jù)庫(kù)提供程序?qū)⑦x擇最適合的默認(rèn)架構(gòu)。例如,Microsoft SQL Server將使用dbo架構(gòu),而且sqlite將不使用架構(gòu)(因?yàn)閟qlite不支持架構(gòu))。

6.2數(shù)據(jù)注釋

不能使用數(shù)據(jù)批注設(shè)置默認(rèn)架構(gòu)。

6.3Fluent API

可以使用API來(lái)指定默認(rèn)架構(gòu)。

class MyContext : DbContext
{
    public DbSet Blogs { get; set; }
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.HasDefaultSchema("blogging");
    }
}

7.默認(rèn)值

如果插入新行,但沒(méi)有為該列指定值,則列的默認(rèn)值為要插入的值。

7.1約定

按照約定,未配置默認(rèn)值。

7.2數(shù)據(jù)注釋

不能使用數(shù)據(jù)批注設(shè)置默認(rèn)值。

7.3Fluent API

你可以使用API指定屬性的默認(rèn)值。

class MyContext : DbContext
{
    public DbSet Blogs { get; set; }
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity()
            .Property(b => b.Rating)
            .HasDefaultValue(3);
    }
}
public class Blog
{
    public int BlogId { get; set; }
    public string Url { get; set; }
    public int Rating { get; set; }
}

還可以指定用于計(jì)算默認(rèn)值的SQL片段。

class MyContext : DbContext
{
    public DbSet Blogs { get; set; }
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity()
            .Property(b => b.Created)
            .HasDefaultValueSql("getdate()");
    }
}
public class Blog
{
    public int BlogId { get; set; }
    public string Url { get; set; }
    public DateTime Created { get; set; }
}

8.索引(關(guān)系數(shù)據(jù)庫(kù))

關(guān)系數(shù)據(jù)庫(kù)中的索引映射到與實(shí)體框架核心中的索引相同的概念。

8.1約定

按照約定,索引命名為IX__。對(duì)于復(fù)合索引,將成為以下劃線分隔的屬性名稱(chēng)列表。

8.2數(shù)據(jù)注釋

不能使用數(shù)據(jù)批注配置索引。

8.3Fluent API

你可以使用熟知的API來(lái)配置索引的名稱(chēng)。

class MyContext : DbContext
{
    public DbSet Blogs { get; set; }
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity()
            .HasIndex(b => b.Url)
            .HasName("Index_Url");
    }
}
public class Blog
{
    public int BlogId { get; set; }
    public string Url { get; set; }
}

你還可以指定篩選器。

class MyContext : DbContext
{
    public DbSet Blogs { get; set; }
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity()
            .HasIndex(b => b.Url)
            .HasFilter("[Url] IS NOT NULL");
    }
}
public class Blog
{
    public int BlogId { get; set; }
    public string Url { get; set; }
}

使用SQL Server提供程序EF為唯一索引中包含的所有可以為null的列添加"IS NOT NULL"篩選器。若要重寫(xiě)此約定,可以null提供一個(gè)值。

class MyContext : DbContext
{
    public DbSet Blogs { get; set; }
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity()
            .HasIndex(b => b.Url)
            .IsUnique()
            .HasFilter(null);
    }
}
public class Blog
{
    public int BlogId { get; set; }
    public string Url { get; set; }
}

在SQL Server索引中包含列,當(dāng)查詢中的所有列都作為鍵列或非鍵列包含在索引中時(shí),可以通過(guò)包含列配置索引以顯著提高查詢性能。

class MyContext : DbContext
{
    public DbSet Posts { get; set; }
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity()
            .HasIndex(p => p.Url)
            .IncludeProperties(p => new
            {
                p.Title,
                p.PublishedOn
            })
            .HasName("Index_Url_Include_Title_PublishedOn");
    }
}
public class Post
{
    public int PostId { get; set; }
    public string Url { get; set; }
    public string Title { get; set; }
    public DateTime PublishedOn { get; set; }
}

原文鏈接:https://www.cnblogs.com/wzk153/p/11796464.html

欄目分類(lèi)
最近更新