一、多導航屬性配型
1、設定導航屬性方式
publicclass
post
public
string title
public
string content
public user author
public user contributor
}public
class
user
public
string firstname
public
string lastname
[inverseproperty(nameof(post.author))]
//設定反轉導航屬性
public listauthoredposts
[inverseproperty(nameof(post.contributor))]
//設定反轉導航屬性
public listcontributedtoposts
}
在post類設定反轉導航屬性也可以
publicclass
post
public
string title
public
string content
[inverseproperty(nameof(user.authoredposts))]
public user author
[inverseproperty(nameof(user.contributedtoposts))]
public user contributor
}public
class
user
public
string firstname
public
string lastname
public listauthoredposts
public listcontributedtoposts
}
post表會預設生成:「導航屬性名id」 的外來鍵 authorid,contributorid
2.foreignkeyattribute方式設定
publicclass
post
public
string title
public
string content
public user author
public user contributor
}public
class
user
public
string firstname
public
string lastname
public user author
[foreignkey(
"authorid")]
public
string contributorid
[foreignkey(
"contributorid")]
public user contributor
}
3.fluent api方式
modelbuilder.entity().hasone(p => p.author).withmany(u=>u.authoredposts).hasforeignkey("authorid");
modelbuilder.entity
().hasone(p => p.contributor).withmany(u => u.contributedtoposts);
二、fluent api顯示設定外來鍵
1、顯示設定外來鍵
referencecollectionbuilder.hasforeignkey(p =>p.blogforeignkey);modelbuilder.entity
().haskey(c => new
); //復合主鍵設定
referencecollectionbuilder.hasfohasforeignkey(s => new
) //設定有復合主鍵表的外來鍵,依賴主體要定義carstate,carlicenseplate 這兩個復合主鍵的屬性字段
referencecollectionbuilder.hasforeignkey(
"blogid
");
2、顯式設定外來鍵關聯主體主鍵之外的鍵,通過 hasprincipalkey 配置
modelbuilder.entity().hasone(p => p.blog).withmany(b =>b.posts).hasforeignkey(p=> p.blogurl).hasprincipalkey(b =>b.url); //post表blogurl欄位是外來鍵,關聯blog表url欄位
modelbuilder.entity
().hasone(p => p.blog).withmany(b =>b.posts).hasforeignkey(p
=> new ).hasprincipalkey(b => new );
1、資料註解方式
publicclass
blog
public
string url
public blogimage blogimage
}public
class
blogimage
public
byte image
public
string caption
public int blogid //顯示指定外來鍵屬性,設定在blog端也可以
public blog blog}
2、fluent api方式:使用 hasone 和 withone 方法
modelbuilder.entity().hasone(p => p.blogimage).withone(i =>i.blog).hasforeignkey(b => b.blogforeignkey); //hasforeignkey指定設定外來鍵那一端
四、多對多:關係型資料庫中不支援多對多的對映,通過建立中間表連線,使用一對多的方式模擬多對多關係
publicclass
post
public
string title
public
string content
public listposttags
}public
class
tag
public listposttags
} //中間表
public
class
posttag
public post post
public
string tagid
public tag tag
}class
mycontext : dbcontext
public dbsettags
protected
override
void
onmodelcreating(modelbuilder modelbuilder)
); //設定中間表主鍵
modelbuilder.entity
() .hasone(pt =>pt.post)
.withmany(p =>p.posttags)
.hasforeignkey(pt =>pt.postid);
modelbuilder.entity
() .hasone(pt =>pt.tag)
.withmany(t =>t.posttags)
.hasforeignkey(pt =>pt.tagid);
}}
五、自引用關係: 依賴關係和主體實體型別相同的關係
1.模型定義
publicclass
picturecategory
public
string name
public
int? parentcategoryid
public
virtual picturecategory parentcategory //
沒有virtual關鍵字,這會導致導航屬性不能載入
public
virtual listsubcategories
}
2.fluentapi配置:在efcore context中重寫方法onmodelcreating配置雙向關聯(parentcategory 和 subcategories)
protectedoverride
void
onmodelcreating(dbmodelbuilder modelbuilder)
EF Core導航屬性
ef core導航屬性分為三種 集合導航屬性 主表中對子表相關資料的引用 引用導航屬性 子表中對主表資料的引用 反轉導航屬性 乙個導航屬性對應的另一端的導航屬性 微軟的示例 blog是主表,post是子表 public class blog public string url public list...
vue 路由 7 導航守衛》
導航守衛 1 什麼是導航守衛?1 vue router提供的導航守衛主要用來監聽路由的進入和離開。2 vue router提供了beforeeach和aftereach的函式,它們會在路由即將改變前和改變後觸發。2 為什麼要用導航守衛?我們來考慮乙個需求 頁面跳轉時如何改變網頁的標題呢?網頁標題是通...
Dapper實現一對多物件關係聚合導航屬性
領域物件 game 遊戲 room 遊戲群 兩者一對多的關係,sql語句中會用到join public class game aggregateroot public string title public string description public ienumerablerooms pub...