EF 笔记
1. 類與類之間通過引用,即為導航屬性,來建立關系,CF會根據引用的屬性類型,將其映射為一對一,或一對多,或多對多的關系
表名生成方式:1)類名的復數(默認的)2)使用特性[Table("TableName")]指定
3)不想使用表名復數時,可以在OnModelCreating 中加入modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();?????
外鍵生成方式:1)表名+ID(默認的)2)使用特性[ForeignKey("FKName")]指定
2. 當一個類在加載數據時,默認不會加載外鍵表數據(懶惰加載:導航屬性由virtual修飾和?Configuration.LazyLoadingEnabled?=?true)
其它兩種加載方式:
貪婪加載:?context.Table1.Include("Table2"), 生成SQL語句,只有一條,相對復雜點
顯示加載: context.Entry(context.Table1.Single()).Collection(d=>d.Table2).Load();?
- 注意結合 Query() 和 IsLoaded 的使用,Collection 是一對多的引用,一對一引用,則使用 Reference
- 執行Load()之后,所有有關Table2的操作都會在內存中進行
- 這里生成的是兩條SQL,執行兩次查詢
3. 當兩類之間多次引用對方時,則會建立組合外鍵,如何確定關系時,則可在一個類的引用屬性上使用InverseProperty("另一類的引用屬性名")
4. 當兩類之間互相引用對方的類集合時,CF將自動生成包含兩類主鍵的中間表,為兩表建立一個多對多的關系
5. 當兩類之間互相引用對方的類時,則需在當前類的主鍵上,設置特性,并把 ForeignKey 指向其引用屬性名(另一個類的名稱,兩表之間為一對一關系
[Key,ForeginKey("另一個類的名稱")]
public Guid 主鍵名稱 //標識特性Key時,其名稱命名可以不用與另一個類的名稱作為前綴
6. one to one:在設置外鍵對象的時候,其它外鍵的對象跟主表對象 必須都來之同一個上下文
void AddProject(){
NSContext context = new NSContext();
?? IRepository<EmailConfig> emailconfigDAL = new NS.Context.EmailConfigDAL(context);//必須傳進去,如果不是同一個上下文,將會報 Cannot insert duplicate key in object “PK_EmailConfig”
???EmailConfig emailconfig = emailconfigDAL.FindAll(new EmailConfig { ID = model.EmailConfig_ID.Value }).First();
?? model.ID = Guid.NewGuid();
???model.EmailConfig = emailconfig;//這里
?? model.CreateDate = DateTime.Now;
???model.WorkStatus = model.WorkStatus;
?? context.Projects.Add(model);
?? context.SaveChanges();
}
7.PM> Enable-Migrations?
8. 數據庫配置,通過 Fluent API 和 Data Annotation 這兩種方式來配置的,?在實際操作中,我們二選一就行
Data Annotation:通過特性的形式來配置,如:
KeyAttribute
StringLengthAttribute
MaxLengthAttribute
ConcurrencyCheckAttribute
RequiredAttribute
TimestampAttribute
ComplexTypeAttribute
ColumnAttribute
TableAttribute
InversePropertyAttribute?
ForeignKeyAttribute
DatabaseGeneratedAttribute?
NotMappedAttribute ?
?
Fluent API:通過在DbContext中的重寫 OnModelCreating 來配置的,為避免該方法體過度膨脹,可以為各個類配置相應映射類,如:
public class 類的名稱Map : EntityTypeConfiguration<類的名稱>{public 類的名稱Map(){Property(d => d.Name).IsRequired();// 這只配置一個要求,如有其它,則可以在自身的映射類中配置}}
然后,
protected override void OnModelCreating(DbModelBuilder modelBuilder){modelBuilder.Configurations.Add(new 類的名稱Map());// 這只體現一行代碼}如下進行配置,如果有多個配置的話,則會膨脹
protected override void OnModelCreating(DbModelBuilder modelBuilder)
??????? {
??????????? modelBuilder.Entity<類的名稱>().Property(d => d.Name).IsRequired();
??????? }
? 另外如果不想表與表之間存在級聯刪除公約,則可以加入
modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
還有其它可以移除的公約(根據需要而定),如:
OneToOneConstraintIntroductionConvention,IncludeMetadataConvention 等等
?
9.?There is already an open DataReader associated with this Command which must be closed first. 異常
由于導航屬性 xxx(具有 virtual 和?LazyLoadingEnabled?=?true), 在 connectionString 中沒有設置?MultipleActiveResultSets=true 引起的
MARS is compatible with SQL Server 2005 and above
10. 樂觀并發控制:
行版本控制:Version 字段數據類型必須為 byte[] , 然后?this.Property(s?=>?s.Version).IsRowVersion() ,在生成的SQL語句中,將會附帶 version='#########' 作為條件,
如:update table set name='xxx' where id=1 and version='########'
字段控制: 任意字段都可以這樣配置?this.Property(s?=>?s.Email).IsConcurrencyToken(),在生成的SQL語句中,將會附帶舊值作為條件,
?如: update table set Email='新值' where id=1 and Email='舊值'
轉載于:https://www.cnblogs.com/yipeng-yu/archive/2013/05/18/3085109.html
總結
- 上一篇: [z]如何在一台windows主机上安装
- 下一篇: 10派16是什么意思