freepeople性欧美熟妇, 色戒完整版无删减158分钟hd, 无码精品国产vα在线观看DVD, 丰满少妇伦精品无码专区在线观看,艾栗栗与纹身男宾馆3p50分钟,国产AV片在线观看,黑人与美女高潮,18岁女RAPPERDISSSUBS,国产手机在机看影片

正文內(nèi)容

efcodefirst學習筆記-閱讀頁

2025-08-10 02:44本頁面
  

【正文】 public PersonPhoto Photo { get。 } } public class PersonPhoto { [Key, ForeignKey(PhotoOf)] public int PersonId { get。 } public byte[] Photo { get。 } public string Caption { get。 } public Person PhotoOf { get。 } }使用Fluent API:PersonPhoto().HasRequired(p = ).WithOptional(p = )。注意:PersonPhoto表中的PersonId既是外鍵也必須是主鍵Entity Framework Code First級聯(lián)刪除 使用Data Annotations:如果我們要到一對主從表增加級聯(lián)刪除,則要在主表中的引用屬性上增加Required關(guān)鍵字,如: public class Destination { public int DestinationId { get。 } public string Name { get。 } public string Country { get。 } public string Description { get。 } public byte[] Photo { get。 } public ListLodging Lodgings { get。 } } public class Lodging { public int LodgingId { get。 } public string Name { get。 } public string Owner { get。 } public bool IsResort { get。 } public decimal MilesFromNearestAirport { get。 } [Required] public Destination Destination { get。 } } 可以看到,在生成的數(shù)據(jù)庫中,外鍵應用了級聯(lián)刪除規(guī)則使用Fluent API:Lodging().HasRequired(l = ).WithMany(d = ).WillCascadeOnDelete(true)。相對應的,如果要關(guān)閉級聯(lián)功能則為:Lodging().HasRequired(l = ).WithMany(d = ).WillCascadeOnDelete(false)。 實體必須是一對一關(guān)系 set。 set。 set。 set。 set。 set。 set。 set。 set。 set。然后我又把模型改了一下: [Table(People)] public class Person { [Key, ForeignKey(Photo)] public int PersonId { get。 } public int SocialSecurityNumber { get。 } public string FirstName { get。 } public string LastName { get。 } [Timestamp] public byte[] RowVersion { get。 } public PersonPhoto Photo { get。 } } [Table(People)] public class PersonPhoto { [Key, ForeignKey(PhotoOf)] public int PersonId { get。 } public byte[] Photo { get。 } public string Caption { get。 } public Person PhotoOf { get。 } }映射可以成功,成功映射后的表結(jié)構(gòu)如圖:但是在插入數(shù)據(jù)的時候Person類中的Photo屬性不能為空,否則會報錯:遇到了無效數(shù)據(jù)。請檢查 StateEntries 以確定違反約束的源。//可以插入成功Person p1 = new Person() { FirstName = Jhon, LastName = Micheal,SocialSecurityNumber=123,Photo=ph}。將一個實體映射到多張表現(xiàn)在我們反轉(zhuǎn)一下,將一個實體映射到多張表,這可以用Fluent API的Map方法來實現(xiàn),不能使用使用Data Annotations ,因為Data Annotations 沒有屬性子集的概念。 set。 set。 set。 set。 set。 set。 set。 (p = )。 (p = )。 }).Map(m = { (B)。 (p = )。生成的表結(jié)構(gòu)如圖:可以看到,Code First自動的為這兩張表創(chuàng)建了主鍵和外鍵。注意:用Map映射的時候務必不要跳過任何屬性!不然Code First還會自動的創(chuàng)建第三張表,保存那些你遺漏的屬性。繼承類的映射TPH(Table Per Hierarchy)TPH:基類和派生類都映射到同一張表中,通過使用鑒別列來識別是否為子類型。 public class Lodging { public int LodgingId { get。 } [Required] [MaxLength(200)] [MinLength(10)] public string Name { get。 } public string Owner { get。 } public decimal MilesFromNearestAirport { get。 } public int DestinationId { get。 } } public class Resort : Lodging { public string Entertainment { get。 } public string Activities { get。 } }生成的數(shù)據(jù)結(jié)構(gòu)如圖:所以的屬性都映射到同一張表中,包括派生類中的Entertainment,Activities,而且還多了一列:Discriminator。 我們可以插入數(shù)據(jù)測試一下: var lodging = new Lodging { Name = Rainy Day Motel, }。 using (var context = new BreakAwayContext()) { (lodging)。 ()。使用Fluent API定制TPH區(qū)分符字段如果你覺得默認的鑒別列(discriminator)列名不夠直觀的話,我們可以通過Fluent API來配置discriminator列的類型和命名(Data Annotations 沒有標記可用于定制TPH)。 (LodgingType).HasValue(Standard)。 })。如果基類只有一個派生類,我們也可以將鑒別列的數(shù)據(jù)類型設(shè)置為bool值: Lodging().Map(m = { (Lodging)。 }) .MapResort(m = { (IsResort).HasValue(true)。TPT(Table Per Type)TPH將所有層次的類都放在了一個表里,而TPT在一個單獨的表中儲存來自基類的屬性,在派生類定義的附加屬性儲存在另一個表里,并使用外鍵與主表相連接。我們顯示的指定派生類生成的表名即可: [Table(Resorts)] public class Resort : Lodging { public string Entertainment { get。 } public string Activities { get。 } }我們可以看到生成了兩張表,模型中的派生類Resort映射的表中只包括它自已的兩個屬性:Entertainment、Activities,基類映射的表中也只包含它自己的屬性。TPC只能用Fluent API來配置。 }).MapResort(m = { (Resorts)。 })。EF Code First學習筆記:數(shù)據(jù)庫創(chuàng)建 控制數(shù)據(jù)庫的位置默認情況下,數(shù)據(jù)庫是創(chuàng)建在localhost\SQLEXPRESS服務器上,并且默認的數(shù)據(jù)庫名為命名空間+context類名。利用配置文件在配置文件中新加一個連接字符串 connectionStrings add name=BreakAwayContext providerName= connectionString=Server=.\SQLEXPRESS。Trusted_Connection=true / /connectionStrings注意這里連接字符串名稱和我們的context類名相同,都為BreakAwayContext。我們在新增一個連接字符串connectionStrings !add name=BreakAwayContext providerName= connectionString=Server=.\SQLEXPRESS。Trusted_Connection=true / add name=My_Test providerName= connectionString=Server=.。Trusted_Connection=true / /connectionStrings新建的連接串名稱和context類名不同了,所以我們要在BreakAwayContext的構(gòu)造函數(shù)中指名連接串的名稱: public class BreakAwayContext : DbContext { public BreakAwayContext(): base(name=My_Test) { } } 利用DbConnectionDbContext有一個帶DbConnection重載的構(gòu)造函數(shù),說明我們也可以通過DbConnection來定位數(shù)據(jù)庫位置。 var resort = new Resort { Name = Top Notch Resort and Spa, MilesFromNearestAirport = 30, Activities = Spa, Hiking, Skiing, Ballooning, }。 Database=BreakAwayDbConnectionConstructor。 using (var connection = new SqlConnection(cstr)) { using (var context = new BreakAwayContext(connection)) { (lodging)。 } } } catch ( ex) { ( 保存失敗)。 ()。此連接工廠將使用SQL Client()數(shù)據(jù)庫引擎連接到數(shù)據(jù)
點擊復制文檔內(nèi)容
環(huán)評公示相關(guān)推薦
文庫吧 www.dybbs8.com
備案圖鄂ICP備17016276號-1