ASP.NET Core使用EF創(chuàng)建模型(必需和可選屬性、最大長度、并發(fā)標(biāo)記、陰影屬性)
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 DbSet<Blog> Blogs { 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 DbSet<Blog> Blogs { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<Blog>() .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 DbSet<Blog> Blogs { 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 DbSet<Blog> Blogs { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<Blog>() .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 DbSet<Person> People { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<Person>() .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 DbSet<Blog> Blogs { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<Blog>() .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<DateTime>(b, "LastUpdated"));
5.1約定
如果發(fā)現(xiàn)了關(guān)系,但在依賴實(shí)體類中找不到外鍵屬性,則可以按約定創(chuàng)建陰影屬性。在這種情況下,將引入陰影外鍵屬性。影子外鍵屬性將命名<navigation property name><principal key property name>為(指向主體實(shí)體的依賴實(shí)體上的導(dǎo)航用于命名)。如果主體鍵屬性名稱包含導(dǎo)航屬性的名稱,則該名稱將只是<principal key property name>。如果依賴實(shí)體上沒有導(dǎo)航屬性,則會在其位置使用主體類型名稱。
例如,下面的代碼列表將導(dǎo)致BlogId Post向?qū)嶓w引入陰影屬性。
class MyContext : DbContext { public DbSet<Blog> Blogs { get; set; } public DbSet<Post> Posts { get; set; } } public class Blog { public int BlogId { get; set; } public string Url { get; set; } //陰影屬性 public List<Post> 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 DbSet<Blog> Blogs { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<Blog>() //創(chuàng)建陰影屬性 .Property<DateTime>("LastUpdated"); } } public class Blog { public int BlogId { get; set; } public string Url { get; set; } }
到此這篇關(guān)于ASP.NET Core使用EF創(chuàng)建模型的文章就介紹到這了。希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- ASP.NET?Core使用EF查詢數(shù)據(jù)
- ASP.NET?Core使用EF為關(guān)系數(shù)據(jù)庫建模
- ASP.NET?Core使用EF創(chuàng)建模型(索引、備用鍵、繼承、支持字段)
- ASP.NET?Core使用EF創(chuàng)建關(guān)系模型
- 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)文章
.Net中的Junction Points(交接點(diǎn))操作
這篇文章介紹了.Net中的Junction Points(交接點(diǎn))操作,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06asp.net實(shí)現(xiàn)導(dǎo)出DataTable數(shù)據(jù)到Word或者Excel的方法
這篇文章主要介紹了asp.net實(shí)現(xiàn)導(dǎo)出DataTable數(shù)據(jù)到Word或者Excel的方法,涉及asp.net操作office文件的相關(guān)技巧,需要的朋友可以參考下2016-08-08WPF集合控件實(shí)現(xiàn)分隔符(ItemsControl Separator)
這篇文章主要為大家詳細(xì)介紹了WPF集合控件實(shí)現(xiàn)分隔符ItemsControl Separator,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04ASP.NET中畫圖形驗(yàn)證碼的實(shí)現(xiàn)代碼
這篇文章給大家介紹了asp.net中畫圖形驗(yàn)證碼的實(shí)現(xiàn)方法,代碼簡單易懂,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下2017-01-01使用.NET Core創(chuàng)建exe應(yīng)用程序
這篇文章介紹了使用.NET Core創(chuàng)建exe應(yīng)用程序的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06基于.NET BitmapImage 內(nèi)存釋放問題的解決方法詳解
本篇文章是對.NET BitmapImage 內(nèi)存釋放問題的解決方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05