ASP.NET?Core使用EF為關(guān)系數(shù)據(jù)庫建模
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í)有所幫助,也希望大家多多支持腳本之家。
- 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ù)、級(jí)聯(lián)刪除和事務(wù)使用
相關(guān)文章
ASP.NET Core 2.0 WebApi全局配置及日志實(shí)例
下面小編就為大家分享一篇ASP.NET Core 2.0 WebApi全局配置及日志實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2017-12-12在應(yīng)用程序級(jí)別之外使用注冊為allowDefinition=''MachineToApplication''的節(jié)是錯(cuò)誤的
在應(yīng)用程序級(jí)別之外使用注冊為 allowDefinition='MachineToApplication' 的節(jié)是錯(cuò)誤的2009-03-03在dropDownList中實(shí)現(xiàn)既能輸入一個(gè)新值又能實(shí)現(xiàn)下拉選的代碼
在dropDownList中實(shí)現(xiàn)既能輸入一個(gè)新值,又能實(shí)現(xiàn)下拉選項(xiàng),想必很多的朋友已經(jīng)為此功能按耐不住了吧,接下來與大家分享下如何實(shí)現(xiàn),感興趣的朋友可以參考下哈2013-04-04Asp.net ajax實(shí)現(xiàn)任務(wù)提示頁面的簡單代碼
這篇文章介紹了Asp.net ajax實(shí)現(xiàn)任務(wù)提示頁面的簡單代碼,有需要的朋友可以參考一下2013-11-11.Net6.0+Vue3實(shí)現(xiàn)數(shù)據(jù)簡易導(dǎo)入功能全過程
最近在用VUE做一個(gè)數(shù)據(jù)導(dǎo)入的功能,下面這篇文章主要給大家介紹了關(guān)于使用.Net6.0+Vue3實(shí)現(xiàn)數(shù)據(jù)簡易導(dǎo)入功能的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-09-09asp.net兩級(jí)聯(lián)動(dòng)(包含添加和修改)
兩級(jí)聯(lián)動(dòng)實(shí)現(xiàn)代碼2009-01-01