我正在創建一個WebAPI,前端將發回實體列表。其中一些可能已經存在。使輸入實體與現有實體匹配?
public void AddTags(List<Tag> tags) //input coming from frontend js
{
foreach(Tag tag in tags){
//check if tag exists in db
//if not, create one
}
}
我試圖用DbContext.Tags.Contains(tag)
但我不知道如何與數據庫中的現有標籤實體鏈接tag
。
我試圖做同樣的事情下面:
Tag tag = new Tag();
tag.name = "foo";
tag.someProp1= "hello";
tag.someProp2= "world";
tag.someProp3= "123";
//tag.id = unique id is unknown, decided by db
//find an identical entity exists in db, link `tag` to it
//if no such entity exists, add to DbContext.Tags
我在尋找類似DbContext.Tags.Match(tag);
凡給出一個實體,它在尋找一個相同的數據庫。
「標籤」是否包含唯一標識符? –
是的,但我相信當它在js端創建時,uid爲空。它與'Tag tag = new Tag()'類似,您可以在其中填寫tag_name,但在執行SaveChanges()之前,不會設置tag_id。 – Bonk