Code First 資料注釋

2022-02-19 09:03:09 字數 1403 閱讀 2383

public class blogdetails   

[maxlength(250)]

public string description

}

請注意,blogdetails 沒有任何鍵屬性型別。在域驅動的設計中,blogdetails 稱為值物件。實體框架將值物件稱為複雜型別。複雜型別不能自行跟蹤。

但是 blogdetails 作為 blog 類中的乙個屬性,將作為 blog 物件的一部分被跟蹤。為了讓 code first 認識到這一點,您必須將 blogdetails 類標記為 complextype。

[complextype] 

public class blogdetails

[maxlength(250)]

public string description

}

concurrencycheck注釋可用於標記要在使用者編輯或刪除實體時用於在資料庫中進行併發檢查的乙個或多個屬性。即在資料中篩選時還將加上標記了該特性的字段。

使用 rowversion 或 timestamp 欄位來進行併發檢查更為常見。code first 將 timestamp 屬性與 concurrencycheck 屬性同等對待,但它還將確保 code first 生成的資料庫欄位是不可為空的。在乙個指定類中,只能有乙個 timestamp 屬性。

[timestamp] 

public byte timestamp

生成資料庫時,code first 會在 post 類中看到 blogid 屬性並識別出該屬性,按照約定,它與類名加「id」匹配,並作為 blog 類的外來鍵。但是在此 blog 類中沒有 blogid 屬性。解決方法是,在 post 中建立乙個導航屬性,並使用 foreign dataannotation 來幫助 code first 了解如何在兩個類之間建立關係(那就是使用 post.blogid 屬性)以及如何在資料庫中指定約束。

public class post 

public string title

public datetime datecreated

public string content

public int blogid

[foreignkey("blogid")]

public blog blog

public icollectioncomments

}

將字段設定為遞增的 databasegenerated.identity

某個欄位是計算得到的 databasegenerated.computed

取消 databasegenerated.none

code first 資料遷移,表 ,資料修改

原文 entity framework codefirst資料遷移 前言緊接著前面一篇博文entity framework codefirst嘗試。我們知道無論是 database first 還是 model first 當模型發生改變了都可以通過visual studio設計檢視進行更新,那麼對...

EF 的 code first 資料遷移

當開發過程中,由類生成了資料庫。但是,在後續的開發過程,要更改某個資料庫的表的列或是增加乙個資料表的時候。ef6.0的操作分別為 為類新增列 比如 cuser 類有name adress兩列。現在需要新增 school一列。使用code first 進行 遷移 migration,步驟如下 1 在v...

通過Code First建立新資料庫

code first建立資料庫的方法十分簡單 易行,先寫好 系統自動生成相應的資料庫框架,讓程式設計師可以更好的物件導向。這次碰到了許多的問題,最主要的乙個問題就是對模型進行更改,當對程式包管理器控制台輸入enable migrations 在尋求多種構建資料模型轉移的方法未果之後,發現了乙個比較合...