欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2022年04月07日 17:30:45   作者:暗斷腸  
這篇文章介紹了ASP.NET?Core使用EF為關(guān)系數(shù)據(jù)庫建模的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

1.簡介

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

2.表映射

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

2.1約定

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

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

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

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

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

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

2.3Fluent API

你可以使用熟知的API來配置類型映射到的表。

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

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

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

3.列映射

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

3.1約定

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

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

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

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

3.3Fluent API

您可以使用熟知的API來配置屬性映射到的列。

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

4.數(shù)據(jù)類型

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

4.1約定

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

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

您可以使用數(shù)據(jù)注釋為列指定精確的數(shù)據(jù)類型。例如,下面的代碼將Url配置為一個(gè)非unicode字符串,其最大200長度。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ù)類型。

class MyContext : DbContext
{
    public DbSet<Blog> Blogs { get; set; }
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Blog>(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í)體類型的鍵引入primary key(主鍵)約束。

5.1約定

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

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

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

5.3Fluent API

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

class MyContext : DbContext
{
    public DbSet<Blog> Blogs { get; set; }
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Blog>()
            .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)

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

6.1約定

按照約定,數(shù)據(jù)庫提供程序?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來指定默認(rèn)架構(gòu)。

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

7.默認(rèn)值

如果插入新行,但沒有為該列指定值,則列的默認(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<Blog> Blogs { get; set; }
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Blog>()
            .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<Blog> Blogs { get; set; }
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Blog>()
            .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ù)庫)

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

8.1約定

按照約定,索引命名為IX_<type name>_<property name>。對(duì)于復(fù)合索引<property name>,將成為以下劃線分隔的屬性名稱列表。

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

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

8.3Fluent API

你可以使用熟知的API來配置索引的名稱。

class MyContext : DbContext
{
    public DbSet<Blog> Blogs { get; set; }
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Blog>()
            .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<Blog> Blogs { get; set; }
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Blog>()
            .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"篩選器。若要重寫此約定,可以null提供一個(gè)值。

class MyContext : DbContext
{
    public DbSet<Blog> Blogs { get; set; }
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Blog>()
            .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í),可以通過包含列配置索引以顯著提高查詢性能。

class MyContext : DbContext
{
    public DbSet<Post> Posts { get; set; }
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Post>()
            .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; }
}

到此這篇關(guān)于ASP.NET Core使用EF為關(guān)系數(shù)據(jù)庫建模的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論