要更改EF中的默認(rèn)配置有兩個(gè)方法,一個(gè)是用Data Annotations(在命名空間System.ComponentModel.DataAnnotations;),直接作用于類的屬性上面;還有一個(gè)就是Fluent API,通過新增相應(yīng)的配置類來覆蓋默認(rèn)配置?,F(xiàn)在我們用這兩個(gè)來對(duì)比了解EF中的約定配置。
為安多等地區(qū)用戶提供了全套網(wǎng)頁設(shè)計(jì)制作服務(wù),及安多網(wǎng)站建設(shè)行業(yè)解決方案。主營業(yè)務(wù)為成都網(wǎng)站建設(shè)、成都網(wǎng)站制作、安多網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會(huì)得到認(rèn)可,從而選擇與我們長期合作。這樣,我們也可以走得更遠(yuǎn)!主鍵:KEY
Data Annotations:通過Key關(guān)鍵字來標(biāo)識(shí)一個(gè)主鍵
[Key]
public int DestinationId { get; set; }
Fluent API:
public class BreakAwayContext : DbContext
{
public DbSet<Destination> Destinations { get; set; }
public DbSet<Lodging> Lodgings { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
//Fluent API modelBuilder.Entity<Destination>().HasKey(d => d.DestinationId);
base.OnModelCreating(modelBuilder);
}
}
外鍵
Data Annotations:
public int DestinationId { get; set; }
[ForeignKey("DestinationId")]
public Destination Destination { get; set; }
注意,指定列名存在,如上面的DestinationId,則類中必須存在名稱為DestinationId的屬性。
Fluent API:
modelBuilder.Entity<Lodging>().HasRequired(p => p.Destination).WithMany(p=>p.Lodgings).HasForeignKey(p => p.DestinationId);
長度
Data Annotations:通過StringLength(長度),MinLength(最小長度),MaxLength(大長度)來設(shè)置數(shù)據(jù)庫中字段的長度。
[MinLength(10),MaxLength(30)]
public string Name { get; set; }
[StringLength(30)]
public string Country { get; set; }
Fluent API:沒有設(shè)置最小長度這個(gè)方法。
modelBuilder.Entity<Destination>().Property(p => p.Name).HasMaxLength(30);
modelBuilder.Entity<Destination>().Property(p => p.Country).HasMaxLength(30);
非空
Data Annotations:用Required來標(biāo)識(shí),還可以設(shè)置是否可允許空字符串,顯示錯(cuò)誤消息等。
[Required]
public string Country { get; set; }
[Required(ErrorMessage="請(qǐng)輸入描述")]
public string Description { get; set; }
Fluent API:
modelBuilder.Entity<Destination>().Property(p => p.Country).IsRequired();
數(shù)據(jù)類型
Data Annotations:TypeName
//將string映射成ntext,默認(rèn)為nvarchar(max) [Column(TypeName = "ntext")]
public string Owner { get; set; }
Fluent API:
modelBuilder.Entity<Lodging>().Property(p => p.Owner).HasColumnType("ntext");
表名
Data Annotations:Table
[Table("MyLodging")]
public class Lodging
{
public int LodgingId { get; set; }
public string Name { get; set; }
public string Owner { get; set; }
public decimal Price { get; set; }
public bool IsResort { get; set; }
public Destination Destination { get; set; }
}
Fluent API:
modelBuilder.Entity<Lodging>().ToTable("MyLodging");
列名
Data Annotations:Column
[Column("MyName")]
public string Name { get; set; }
Fluent API:
modelBuilder.Entity<Lodging>().Property(p => p.Name).HasColumnName("MyName");
自增長
如果主鍵是int類型,EF為默認(rèn)設(shè)置為增長。但如果是GUID類型,則要顯示的設(shè)置自增長。
Data Annotations:DatabaseGenerated
public class Person
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid SocialId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
看看創(chuàng)建數(shù)據(jù)的腳本,會(huì)加一句
ALTER TABLE [dbo].[People] ADDDEFAULT (newid()) FOR [SocialId]
Fluent API:
modelBuilder.Entity<Person>().Property(p => p.SocialId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
忽略列映射
類中有些屬性,特別是一些通過計(jì)算或合并列得出的結(jié)果,我們并不需要其記錄到數(shù)據(jù)庫中,就可以通過配置不讓它生成在數(shù)據(jù)庫中。
Data Annotations:NotMapped
[NotMapped]
public string Name
{
get
{
return FirstName + " " + LastName;
}
}
Fluent API:NotMapped
modelBuilder.Entity<Person>().Ignore(p => p.Name);
忽略表映射
對(duì)于不需要映射到數(shù)據(jù)庫中的表,我們也可以取消其映射。
Data Annotations:
[NotMapped]
public class Person
{
[Key]
public Guid SocialId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
Fluent API:
modelBuilder.Ignore<Person>();
時(shí)間戳
時(shí)間戳只對(duì)數(shù)據(jù)類型為byte[]的屬性有效,并且一個(gè)類中只能有一個(gè)設(shè)置為時(shí)間戳的屬性。
Data Annotations:Timestamp
1658897004
public Byte[] TimeStamp { get; set; }
Fluent API:
modelBuilder.Entity<Lodging>().Property(p => p.TimeStamp).IsRowVersion();
復(fù)雜類型
Data Annotations:ComplexType
[ComplexType]
public class Address
{
public string Country { get; set; }
public string City { get; set; }
}
Fluent API:
modelBuilder.ComplexType<Address>();
網(wǎng)頁題目:EFCodeFirst學(xué)習(xí)筆記:約定配置-創(chuàng)新互聯(lián)
網(wǎng)頁鏈接:http://aaarwkj.com/article12/piegc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供搜索引擎優(yōu)化、品牌網(wǎng)站設(shè)計(jì)、網(wǎng)站設(shè)計(jì)公司、網(wǎng)站建設(shè)、電子商務(wù)、手機(jī)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)