網(wǎng)站首頁(yè) 編程語(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
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 DbSetBlogs { 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 DbSetBlogs { 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 DbSetBlogs { 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 DbSetBlogs { 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 DbSetBlogs { 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 DbSetBlogs { 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 DbSetBlogs { 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 DbSetBlogs { 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_
8.2數(shù)據(jù)注釋
不能使用數(shù)據(jù)批注配置索引。
8.3Fluent API
你可以使用熟知的API來(lái)配置索引的名稱(chēng)。
class MyContext : DbContext { public DbSetBlogs { 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 DbSetBlogs { 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 DbSetBlogs { 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 DbSetPosts { 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
相關(guān)推薦
- 2022-11-30 C語(yǔ)言中順序棧和鏈棧的定義和使用詳解_C 語(yǔ)言
- 2022-03-26 C++?Primer學(xué)習(xí)記錄之變量_C 語(yǔ)言
- 2022-04-09 SpringMVC 使用RestFul風(fēng)格實(shí)現(xiàn)簡(jiǎn)單的文件下載
- 2023-04-01 SqlServer字符截取的具體函數(shù)使用_MsSql
- 2022-06-12 C語(yǔ)言?深入探究動(dòng)態(tài)規(guī)劃之區(qū)間DP_C 語(yǔ)言
- 2022-12-22 C++?Boost?Foreach超詳細(xì)分析講解_C 語(yǔ)言
- 2022-12-06 Python中八種數(shù)據(jù)導(dǎo)入方法總結(jié)_python
- 2022-07-28 ?python中的元類(lèi)metaclass詳情_(kāi)python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門(mén)
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支