我想首先使用 EF Core 代碼對以下這些模型執行一些簡單的查詢。
Post模型
public class Post
{
public int Id { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public int FirstTagId { get; set; }
public FirstTag FirstTag { get; set; }
public int SecondTagId { get; set; }
public SecondTag SecondTag { get; set; }
}
FirstTag模型
public class FirstTag
{
public int Id { get; set; }
public string tagName { get; set; }
public ICollection<Post> Post { get; set; }
}
SecondTag模型
public class SecondTag
{
public int Id { get; set; }
public string tagName { get; set; }
public ICollection<Post> Post { get; set; }
}
我的意圖是當我創建一個新帖子時。我想使用以下檢查操作檢查標簽是否會插入到新帖子中
[HttpPost]
public async Task<ActionResult<Post>> CreatePost([FromBody] NewPostParam postParam)
{
// to prevent inserting duplicate first tag
var tag1 = _context.FirstTags.Where(x =>
(string.IsNullOrEmpty(postParam.Post.FirstTag.tagName) || x.tagName == postParam.Post.FirstTag.tagName))
.FirstOrDefault();
// to prevent inserting duplicate second tag
var tag2 = _context.SecondTags.Where(x =>
(string.IsNullOrEmpty(postParam.Post.SecondTag.tagName) || x.tagName == postParam.Post.SecondTag.tagName))
.FirstOrDefault();
if (tag1 == null)
{
tag1 = new FirstTag
{
tagName = "tag1 test",
tagDescription = "tag1 test des!!"
};
_context.FirstTags.Add(tag1);
}
if (tag2 == null)
{
tag2 = new SecondTag
{
tagName = "tag2 test",
tagDescription = "tag2 test des!"
};
_context.SecondTags.Add(tag2);
}
var newPost = new Post
{
Title = postParam.Post.Title,
Description = postParam.Post.Description,
FirstTag = tag1 != null ? tag1 : postParam.Post.FirstTag,
SecondTag = tag2 != null ? tag2 : postParam.Post.SecondTag,
};
_context.Posts.Add(newPost);
// remain code ...
}
這是當前存在的標簽資料庫
FirstTag桌子
Id | tagName
1 firstTag1
2 firstTag2
SecondTag桌子
Id | tagName
1 secondTag1
2 secondTag2
這是我用于添加新帖子的 JSON 資料
{
"post": {
"title": "post2 test titlte",
"description": "post2 test description!",
"firstTag": {
"tagName": "firstTag1",
"tagDescription": "firstTag des!!"
},
"secondTag": {
"tagName": "secondTag2",
"tagDescription": "secondTag des!!"
}
}
}
我已嘗試多次為FirstTagand添加相同的名稱SecondTag。但是新資料剛剛創建,我上面執行的檢查操作可能沒有阻止這種情況發生。如何防止插入重復資料?
uj5u.com熱心網友回復:
忽略您設計的一些有問題的方面,試試這個:
if(!string.IsNullOrEmpty(postParam.Post.FirstTag.tagName))
{
if(!_context.FirstTags.Any(t => t.tagName.Trim().ToLower() == postParam.Post.FirstTag.tagName.Trim().ToLower()))
{
var tag1 = new FirstTag
{
tagName = "tag1 test",
tagDescription = "tag1 test des!!"
};
_context.FirstTags.Add(tag1);
}
}
if (!string.IsNullOrEmpty(postParam.Post.SecondTag.tagName))
{
if (!_context.FirstTags.Any(t => t.tagName.Trim().ToLower() == postParam.Post.SecondTag.tagName.Trim().ToLower()))
{
var tag2 = new FirstTag
{
tagName = "tag1 test",
tagDescription = "tag1 test des!!"
};
_context.SecondTags.Add(tag2);
}
}
如您所見,我將模型值的驗證與查詢分開。我個人不喜歡使事情難以理解的令人費解的單行詞。
代碼檢查是否有匹配,考慮到填充和大小寫(因為我對你的后端不太了解)。
回到有問題的設計。注意代碼是如何可疑地重復的?(提示:這與標簽是標簽這一事實有關,無論其順序或數量如何。)
uj5u.com熱心網友回復:
您可以按如下方式檢查重復記錄
var isDuplicate = _context.FirstTags.Any(x =>x.tagName == postParam.Post.FirstTag.tagName)
if(isDuplicate){
//Do something
}
uj5u.com熱心網友回復:
除非您沒有從您的資料模型中發布某些內容,FirstTag并且SecondTag應該是相同的模型,或者至少從相同的基本模型繼承。
您的課程Post不包含 的定義,SecondTag但我假設它與FirstTag. 您Id的 JSON 示例中也缺少您的內容。
忽略這些不一致,您首先在CreatePost:tag1和tag2. 由于postParam沒有填充標簽,因此這兩個變數都將初始化為null. 因此,兩個 if 陳述句都將驗證為是合乎邏輯的true
因此,您想要做的更多的是:
[HttpPost]
public async Task<ActionResult<Post>> CreatePost([FromBody] NewPostParam postParam)
{
// to prevent inserting duplicate first tag
var tag1 = _context.FirstTags.Where(x =>
(string.IsNullOrEmpty(postParam.Post.FirstTag.tagName) || x.tagName == postParam.Post.FirstTag.tagName))
.FirstOrDefault();
SecondTag tag2 = null;
if (tag1 == null)
{
tag1 = new FirstTag
{
tagName = "tag1 test",
tagDescription = "tag1 test des!!"
};
_context.FirstTags.Add(tag1);
}
else
{
tag2 = _context.SecondTags.Where(x =>
(string.IsNullOrEmpty(postParam.Post.SecondTag.tagName) || x.tagName == postParam.Post.SecondTag.tagName))
.FirstOrDefault();
if (tag2 == null)
{
tag2 = new SecondTag
{
tagName = "tag2 test",
tagDescription = "tag2 test des!"
};
_context.SecondTags.Add(tag2);
}
}
var newPost = new Post
{
Title = postParam.Post.Title,
Description = postParam.Post.Description,
FirstTag = tag1 != null ? tag1 : postParam.Post.FirstTag,
SecondTag = tag2 != null ? tag2 : postParam.Post.SecondTag,
};
_context.Posts.Add(newPost);
// remain code ...
}
其次,您沒有檢查是否tag1已經具有類似tag2. 如果您從不希望 tag2 填充 tag1 的標簽,請執行以下操作:
tag2 = _context.SecondTags.FirstOrDefault(x =>
string.IsNullOrEmpty(postParam.Post.SecondTag.tagName) || (x.tagName == postParam.Post.SecondTag.tagName && postParam.Post.FirstTag.tagName != x.tagName));
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/453232.html
