ASP.NET Core使用EF創(chuàng)建模型(必需和可選屬性、最大長度、并發(fā)標(biāo)記、陰影屬性)
1.必需和可選屬性
如果實體屬性可以包含null,則將其視為可選。如果屬性的有效值不可以包含null,則將其視為必需屬性。映射到關(guān)系數(shù)據(jù)庫架構(gòu)時,必需的屬性將創(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; }
//加上這個批注,這個值就必需寫入
[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)
//這個方法表示必需寫入
.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ù)庫提供程序為屬性選擇適當(dāng)?shù)臄?shù)據(jù)類型,即數(shù)據(jù)庫字段設(shè)置長度多少,生產(chǎn)程序?qū)嶓w接受值時就限定長度多少。對于具有長度的屬性,數(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)境某個實體字段經(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ù)據(jù)庫都會生成一個新值。此該屬性類型也被視為并發(fā)標(biāo)記。這可以確保在你和其他人修改了行數(shù)據(jù)時你會收到異常信息。
4.1約定
按照約定,屬性永遠(yuǎn)不會配置為時間戳。
4.2數(shù)據(jù)注釋
你可以使用數(shù)據(jù)批注將屬性配置為時間戳。
public class Blog
{
public int BlogId { get; set; }
public string Url { get; set; }
//設(shè)置時間戳
[Timestamp]
public byte[] Timestamp { get; set; }
}4.3Fluent API
你可以使用熟知的API將屬性配置為時間戳。
class MyContext : DbContext
{
public DbSet<Blog> Blogs { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Blog>()
.Property(p => p.Timestamp)
//設(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)在映射的實體類型上公開時,陰影屬性非常有用。它們最常用于外鍵屬性,其中兩個實體之間的關(guān)系由數(shù)據(jù)庫中的外鍵值表示,但使用實體類型之間的導(dǎo)航屬性在實體類型上管理關(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)系,但在依賴實體類中找不到外鍵屬性,則可以按約定創(chuàng)建陰影屬性。在這種情況下,將引入陰影外鍵屬性。影子外鍵屬性將命名<navigation property name><principal key property name>為(指向主體實體的依賴實體上的導(dǎo)航用于命名)。如果主體鍵屬性名稱包含導(dǎo)航屬性的名稱,則該名稱將只是<principal key property name>。如果依賴實體上沒有導(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)有屬性的名稱相匹配(一個陰影屬性或在實體類中定義的屬性),則代碼將配置該現(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)文章
ASP.NET Core中預(yù)壓縮靜態(tài)文件的方法步驟
這篇文章主要給大家介紹了關(guān)于ASP.NET Core中如何預(yù)壓縮靜態(tài)文件的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用ASP.NET Core具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03
.NET獲取枚舉DescriptionAttribute描述信息性能改進(jìn)的多種方法
這篇文章主要介紹了.NET獲取枚舉DescriptionAttribute描述信息性能改進(jìn)的多種方法 的相關(guān)資料,需要的朋友可以參考下2016-01-01
Visual?Studio?2022?MAUI?NU1105(NETSDK1005)?問題處理記錄
某一天修改了幾行代碼后,突然項目無法編譯了,提示NU1105錯誤,這篇文章主要介紹了Visual?Studio?2022?MAUI?NU1105(NETSDK1005)?處理記錄,需要的朋友可以參考下2022-12-12
基于localStorge開發(fā)登錄模塊的記住密碼與自動登錄實例
下面小編就為大家?guī)硪黄趌ocalStorge開發(fā)登錄模塊的記住密碼與自動登錄實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-08-08
淺談對Jquery+JSON+WebService的使用小結(jié)
本篇文章介紹了對Jquery+JSON+WebService的使用小結(jié)。需要的朋友參考下2013-04-04
asp.net下String.prototype.split()的兼容問題
IE下的String.prototype.split()函數(shù)bug2012-12-12

