網(wǎng)站首頁 編程語言 正文
ASP.NET?Core使用EF創(chuàng)建模型(必需和可選屬性、最大長度、并發(fā)標(biāo)記、陰影屬性)_實(shí)用技巧
作者:暗斷腸 ? 更新時(shí)間: 2022-06-09 編程語言1.必需和可選屬性
如果實(shí)體屬性可以包含null,則將其視為可選。如果屬性的有效值不可以包含null,則將其視為必需屬性。映射到關(guān)系數(shù)據(jù)庫架構(gòu)時(shí),必需的屬性將創(chuàng)建為不可為null的列,而可選屬性則創(chuàng)建為可以為null的列。
1.1約定
按照約定,.NET 類型可以包含null的屬性將配置為可選,而.NET類型不包含null的屬性將根據(jù)需要進(jìn)行配置。例如,具有.net值類型(int、decimal、bool等)的所有屬性都是必需的,而具有可為null的.net值類型(int?、decimal?、bool?等)的所有屬性都是配置為可選。
1.2數(shù)據(jù)批注
可以按如下所示將"約定"可以為"可選"的屬性配置為"必需":
namespace EFModeling.DataAnnotations.Required { class MyContext : DbContext { public DbSetBlogs { get; set; } } public class Blog { public int BlogId { get; set; } //加上這個(gè)批注,這個(gè)值就必需寫入 [Required] public string Url { get; set; } } }
1.3Fluent API
namespace EFModeling.FluentAPI.Required { class MyContext : DbContext { public DbSetBlogs { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity () .Property(b => b.Url) //這個(gè)方法表示必需寫入 .IsRequired(); } } public class Blog { public int BlogId { get; set; } public string Url { get; set; } } }
2.最大長度
配置最大長度可為數(shù)據(jù)存儲提供有關(guān)要對給定屬性使用的相應(yīng)數(shù)據(jù)類型的提示。最大長度僅適用于數(shù)組數(shù)據(jù)類型,如string和byte[]。例如前端傳統(tǒng)數(shù)據(jù)長度遠(yuǎn)大于限定的長度,則提示。
2.1約定
按照約定,應(yīng)由數(shù)據(jù)庫提供程序?yàn)閷傩赃x擇適當(dāng)?shù)臄?shù)據(jù)類型,即數(shù)據(jù)庫字段設(shè)置長度多少,生產(chǎn)程序?qū)嶓w接受值時(shí)就限定長度多少。對于具有長度的屬性,數(shù)據(jù)庫提供程序通常將選擇允許最長數(shù)據(jù)長度的數(shù)據(jù)類型。例如,Microsoft SQL Server將對字符string屬性使用 nvarchar(max)(如果該列用作鍵,則會使用nvarchar(450))。
2.2數(shù)據(jù)批注
你可以使用數(shù)據(jù)批注為屬性配置最大長度。此示例面向SQL Server,因此使用數(shù)據(jù)類型 nvarchar(500)。
namespace EFModeling.DataAnnotations.MaxLength { class MyContext : DbContext { public DbSetBlogs { get; set; } } public class Blog { public int BlogId { get; set; } //設(shè)置最大長度 [MaxLength(500)] public string Url { get; set; } } }
2.3Fluent API
namespace EFModeling.FluentAPI.MaxLength { class MyContext : DbContext { public DbSetBlogs { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity () .Property(b => b.Url) //設(shè)置最大長度 .HasMaxLength(500); } } public class Blog { public int BlogId { get; set; } public string Url { get; set; } } }
3.并發(fā)標(biāo)記
當(dāng)我們發(fā)現(xiàn)生產(chǎn)環(huán)境某個(gè)實(shí)體字段經(jīng)常處于并發(fā)當(dāng)中,我們可以批注一下為并發(fā)字段。
3.1約定
按照約定,屬性永遠(yuǎn)不會配置為并發(fā)標(biāo)記。
3.2數(shù)據(jù)注釋
您可以使用數(shù)據(jù)批注將屬性配置為并發(fā)標(biāo)記。
public class Person { public int PersonId { get; set; } //并發(fā)標(biāo)記 [ConcurrencyCheck] public string LastName { get; set; } public string FirstName { get; set; } }
3.3Fluent API
您可以使用熟知的API將屬性配置為并發(fā)標(biāo)記。
class MyContext : DbContext { public DbSetPeople { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity () .Property(p => p.LastName) //并發(fā)標(biāo)記 .IsConcurrencyToken(); } } public class Person { public int PersonId { get; set; } public string LastName { get; set; } public string FirstName { get; set; } }
4.時(shí)間戳/行版本
時(shí)間戳是一個(gè)屬性類型,在每次插入或更新行時(shí),數(shù)據(jù)庫都會生成一個(gè)新值。此該屬性類型也被視為并發(fā)標(biāo)記。這可以確保在你和其他人修改了行數(shù)據(jù)時(shí)你會收到異常信息。
4.1約定
按照約定,屬性永遠(yuǎn)不會配置為時(shí)間戳。
4.2數(shù)據(jù)注釋
你可以使用數(shù)據(jù)批注將屬性配置為時(shí)間戳。
public class Blog { public int BlogId { get; set; } public string Url { get; set; } //設(shè)置時(shí)間戳 [Timestamp] public byte[] Timestamp { get; set; } }
4.3Fluent API
你可以使用熟知的API將屬性配置為時(shí)間戳。
class MyContext : DbContext { public DbSetBlogs { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity () .Property(p => p.Timestamp) //設(shè)置時(shí)間戳 .IsRowVersion(); } } public class Blog { public int BlogId { get; set; } public string Url { get; set; } public byte[] Timestamp { get; set; } }
5.陰影屬性
當(dāng)數(shù)據(jù)庫中的數(shù)據(jù)不應(yīng)在映射的實(shí)體類型上公開時(shí),陰影屬性非常有用。它們最常用于外鍵屬性,其中兩個(gè)實(shí)體之間的關(guān)系由數(shù)據(jù)庫中的外鍵值表示,但使用實(shí)體類型之間的導(dǎo)航屬性在實(shí)體類型上管理關(guān)系,可以通過ChangeTracker API獲取和更改影子屬性值:
context.Entry(myBlog).Property("LastUpdated").CurrentValue = DateTime.Now;
可以通過EF.Property靜態(tài)方法在LINQ查詢中引用影子屬性:
var blogs = context.Blogs.OrderBy(b => EF.Property(b, "LastUpdated"));
5.1約定
如果發(fā)現(xiàn)了關(guān)系,但在依賴實(shí)體類中找不到外鍵屬性,則可以按約定創(chuàng)建陰影屬性。在這種情況下,將引入陰影外鍵屬性。影子外鍵屬性將命名
例如,下面的代碼列表將導(dǎo)致BlogId Post向?qū)嶓w引入陰影屬性。
class MyContext : DbContext { public DbSetBlogs { get; set; } public DbSet Posts { get; set; } } public class Blog { public int BlogId { get; set; } public string Url { get; set; } //陰影屬性 public List Posts { get; set; } } public class Post { public int PostId { get; set; } public string Title { get; set; } public string Content { get; set; } //陰影屬性 public Blog Blog { get; set; } }
5.2數(shù)據(jù)注釋
不能通過數(shù)據(jù)批注創(chuàng)建陰影屬性。
5.3Fluent API
你可以使用"熟知API"配置陰影屬性。一旦你調(diào)用了Property方法的字符串重載,就可以鏈接到其他屬性的任何配置調(diào)用。如果提供Property方法的名稱與現(xiàn)有屬性的名稱相匹配(一個(gè)陰影屬性或在實(shí)體類中定義的屬性),則代碼將配置該現(xiàn)有屬性,而不是引入新的陰影屬性。
class MyContext : DbContext { public DbSetBlogs { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity () //創(chuàng)建陰影屬性 .Property ("LastUpdated"); } } public class Blog { public int BlogId { get; set; } public string Url { get; set; } }
原文鏈接:https://www.cnblogs.com/wzk153/p/11726106.html
相關(guān)推薦
- 2022-01-30 uniapp蘋果底部欄自適應(yīng)配置
- 2022-05-10 torch.cuda.is_available()返回false最終解決方案
- 2022-09-13 Nginx如何限制IP訪問只允許特定域名訪問_nginx
- 2022-11-06 python?基本結(jié)構(gòu)語句(函數(shù)和模塊)_python
- 2022-11-13 Python?fileinput模塊應(yīng)用詳解_python
- 2022-06-12 C語言?智能指針?shared_ptr?和?weak_ptr_C 語言
- 2022-08-27 C++?Thread實(shí)現(xiàn)簡單的socket多線程通信_C 語言
- 2022-07-08 Android?iOS常用APP崩潰日志獲取命令方法_Android
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- 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)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤: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)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支