对于使用LINQ的查询,我具有以下语法:
list = await _applictationContext.PersonContacts.Where(x => x.PersonId == id).ToListAsync();
但这给我0个结果。
当我这样查询数据库时:
select * from [Person].[Contact] where PersonId = 3
我得到结果。我确定linq的ID中有一个“ 3”。
这是我的ApplicationContext
public DbSet<PersonContact> PersonContacts { get; set; }
这是整个语法:
[HttpPost]
[Route("GetContacts")]
public async Task<IActionResult> GetContacts(IFormCollection formValue)
{
List<PersonContact> list = null;
var id = Convert.ToInt32(formValue["personId"][0]);
try {
list = await _applictationContext.PersonContacts.Where(x => x.PersonId == id).ToListAsync();
// list = await (from x in _applictationContext.PersonContacts
// where x.PersonId == id
// select x).ToListAsync();
return Ok(list);
}catch(Exception ex) {
return BadRequest(list);
}
}
这就是我的更新方式和工作方式
[HttpPost]
[Route("UpdateContact")]
public async Task<IActionResult> UpdateContact(PersonContact model)
{
try{
if (model.ContactId == 0) {
_applictationContext.PersonContacts.Add(model);
_applictationContext.Entry(model).State = EntityState.Added;
} else {
model.DateModified = DateTime.Now;
_applictationContext.PersonContacts.Attach(model);
_applictationContext.Entry(model).State = EntityState.Modified;
}
var status = await _applictationContext.SaveChangesAsync();
return Ok(status);
} catch(Exception ex) {
return BadRequest(ex.Message);
}
}