ASP.NET?Core使用EF為關(guān)系數(shù)據(jù)庫建模
1.簡介
一般而言,本部分中的配置適用于關(guān)系數(shù)據(jù)庫。安裝關(guān)系數(shù)據(jù)庫提供程序時,此處顯示的變?yōu)榭捎脭U(kuò)展方法(原因在于共享的Microsoft.EntityFrameworkCore.Relational包)。
2.表映射
表映射標(biāo)識在數(shù)據(jù)庫中哪張表應(yīng)該進(jìn)行內(nèi)容查詢和保存操作。
2.1約定
按照約定,每個實體將設(shè)置為映射到名稱與DbSet<TEntity> 屬性(公開派生上下文中的實體)相同的表中。如果給定DbSet<TEntity>實體中不包含,則使用類名稱。
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ù)據(jù)庫中應(yīng)從哪些列數(shù)據(jù)中進(jìn)行查詢和保存。
3.1約定
按照約定,每個屬性將會設(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ù)類型。它還會考慮其他元數(shù)據(jù),如配置的最大長度、屬性是否是主鍵的一部分等。例如,SQL Server的DateTime、nvarchar(max) 用作鍵的屬性。
4.2數(shù)據(jù)注釋
您可以使用數(shù)據(jù)注釋為列指定精確的數(shù)據(jù)類型。例如,下面的代碼將Url配置為一個非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.主鍵
為每個實體類型的鍵引入primary key(主鍵)約束。
5.1約定
按照約定,會將數(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)
如果沒有為該對象顯式配置架構(gòu),則默認(rèn)架構(gòu)為將在其中創(chuàng)建對象的數(shù)據(jù)庫架構(gòu)。
6.1約定
按照約定,數(shù)據(jù)庫提供程序?qū)⑦x擇最適合的默認(rèn)架構(gòu)。例如,Microsoft SQL Server將使用dbo架構(gòu),而且sqlite將不使用架構(gòu)(因為sqlite不支持架構(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; }
}還可以指定用于計算默認(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ù)庫中的索引映射到與實體框架核心中的索引相同的概念。
8.1約定
按照約定,索引命名為IX_<type name>_<property name>。對于復(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提供一個值。
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)查詢中的所有列都作為鍵列或非鍵列包含在索引中時,可以通過包含列配置索引以顯著提高查詢性能。
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ù)庫建模的文章就介紹到這了。希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- ASP.NET?Core使用EF查詢數(shù)據(jù)
- ASP.NET?Core使用EF創(chuàng)建模型(索引、備用鍵、繼承、支持字段)
- ASP.NET?Core使用EF創(chuàng)建關(guān)系模型
- ASP.NET Core使用EF創(chuàng)建模型(必需和可選屬性、最大長度、并發(fā)標(biāo)記、陰影屬性)
- ASP.NET?Core使用EF創(chuàng)建模型(包含屬性、排除屬性、主鍵和生成值)
- ASP.NET?Core基于現(xiàn)有數(shù)據(jù)庫創(chuàng)建EF模型
- EF?Core通過顯式編譯提高查詢性能
- ASP.NET Core使用EF保存數(shù)據(jù)、級聯(lián)刪除和事務(wù)使用
相關(guān)文章
ASP.NET Core依賴注入系列教程之服務(wù)的注冊與提供
這篇文章主要給大家介紹了關(guān)于ASP.NET Core依賴注入系列教程之服務(wù)的注冊與提供的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起看看吧2018-11-11
在ASP.NET Core中用HttpClient發(fā)送POST, PUT和DELETE請求
這篇文章主要介紹了在ASP.NET Core中用HttpClient發(fā)送POST, PUT和DELETE請求的方法,幫助大家更好的理解和學(xué)習(xí)使用ASP.NET Core,感興趣的朋友可以了解下2021-03-03
Entity?Framework使用Code?First的實體繼承模式
本文詳細(xì)講解了Entity?Framework使用Code?First的實體繼承模式,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-03-03
ASP.NET對SQLServer的通用數(shù)據(jù)庫訪問類
這篇文章主要實現(xiàn)了ASP.NET對SQLServer的通用數(shù)據(jù)庫訪問類2016-02-02
基于.net core微服務(wù)的另一種實現(xiàn)方法
這篇文章主要給大家介紹了基于.net core微服務(wù)的另一種實現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07
SqlDataReader生成動態(tài)Lambda表達(dá)式
這篇文章主要介紹了SqlDataReader生成動態(tài)Lambda表達(dá)式,需要的朋友可以參考下2017-04-04

