要更改EF中的默認(rèn)配置有兩個(gè)方法,一個(gè)是用Data Annotations(在命名空間System.ComponentModel.DataAnnotations;)褥紫,直接作用于類(lèi)的屬性上面;還有一個(gè)就是Fluent API,通過(guò)新增相應(yīng)的配置類(lèi)來(lái)覆蓋默認(rèn)配置」窗睿現(xiàn)在我們用這兩個(gè)來(lái)對(duì)比了解EF中的約定配置刻蟹。
主鍵:KEY
Data Annotations:通過(guò)Key關(guān)鍵字來(lái)標(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介劫,則類(lèi)中必須存在名稱(chēng)為DestinationId的屬性。
Fluent API:
modelBuilder.Entity<Lodging>().HasRequired(p => p.Destination).WithMany(p=>p.Lodgings).HasForeignKey(p => p.DestinationId);
長(zhǎng)度
Data Annotations:通過(guò)StringLength(長(zhǎng)度),MinLength(最小長(zhǎng)度),MaxLength(最大長(zhǎng)度)來(lái)設(shè)置數(shù)據(jù)庫(kù)中字段的長(zhǎng)度案淋。
[MinLength(10),MaxLength(30)]
public string Name { get; set; }
[StringLength(30)]
public string Country { get; set; }
Fluent API:沒(méi)有設(shè)置最小長(zhǎng)度這個(gè)方法座韵。
modelBuilder.Entity<Destination>().Property(p => p.Name).HasMaxLength(30);
modelBuilder.Entity<Destination>().Property(p => p.Country).HasMaxLength(30);
非空
Data Annotations:用Required來(lái)標(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ù)類(lèi)型
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");
自增長(zhǎng)
如果主鍵是int類(lèi)型,EF為默認(rèn)設(shè)置為增長(zhǎng)黔帕。但如果是GUID類(lèi)型代咸,則要顯示的設(shè)置自增長(zhǎng)。
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] ADD DEFAULT (newid()) FOR [SocialId]
Fluent API:
modelBuilder.Entity<Person>().Property(p => p.SocialId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
忽略列映射
類(lèi)中有些屬性呐芥,特別是一些通過(guò)計(jì)算或合并列得出的結(jié)果,我們并不需要其記錄到數(shù)據(jù)庫(kù)中奋岁,就可以通過(guò)配置不讓它生成在數(shù)據(jù)庫(kù)中思瘟。
Data Annotations:NotMapped
[NotMapped]
public string Name
{
get
{
return FirstName + " " + LastName;
}
}
Fluent API:NotMapped
modelBuilder.Entity<Person>().Ignore(p => p.Name);
忽略表映射
對(duì)于不需要映射到數(shù)據(jù)庫(kù)中的表,我們也可以取消其映射闻伶。
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ù)類(lèi)型為byte[]的屬性有效滨攻,并且一個(gè)類(lèi)中只能有一個(gè)設(shè)置為時(shí)間戳的屬性。
Data Annotations:Timestamp
[Timestamp]
public Byte[] TimeStamp { get; set; }
Fluent API:
modelBuilder.Entity<Lodging>().Property(p => p.TimeStamp).IsRowVersion();
復(fù)雜類(lèi)型
Data Annotations:ComplexType
[ComplexType]
public class Address
{
public string Country { get; set; }
public string City { get; set; }
}
Fluent API:
modelBuilder.ComplexType<Address>();
關(guān)于什么是復(fù)雜類(lèi)型蓝翰,可以參見(jiàn):http://www.cnblogs.com/Gyoung/archive/2013/01/17/2864747.html