我有一个支付模型,其'状态'布尔值默认为false.付款完成后,我需要将特定付款的"状态"更新为true.
这是我一直试图用来更改特定数据库条目的代码,但它只是没有改变它.我究竟做错了什么?
Payment payment = new Payment();
payment = db.Payments.Find(orderId);
db.Entry(payment).State = EntityState.Modified;
payment.Status = true;
db.SaveChanges();
Run Code Online (Sandbox Code Playgroud)
谢谢!
这就是最终的工作:
using (var con = new ApplicationDbContext())
{
payment = con.Payments.First(x => x.Id == orderId);
payment.Status = true;
con.Payments.Attach(payment);
var entry = con.Entry(payment);
entry.Property(e => e.Status).IsModified = true;
con.SaveChanges();
}
Run Code Online (Sandbox Code Playgroud) 是否有可能在1个语句中写这个(只进行1个db调用?)并且仍能区分"成员不存在"和"成员确实存在但没有狗".
public IEnumerable<Dog> GetDogsOfMember(int id)
{
if (dbContext.Members.Any(i => i.ID == id))
{
return dbContext.Dogs.Where(i => i.Member.ID == id);
}
return null;
}
Run Code Online (Sandbox Code Playgroud) 我有以下型号:
case class Questionnaire(List(Question[_])
case class Question[A](text: String, Answer[A])
case class Answer[A](value: A)
Run Code Online (Sandbox Code Playgroud)
我试图根据问卷类型设置问卷的所有答案(答案可以是String,Double或LocalDate类型):
val questionnaire = getMockQuestionnaireWithoutAnswers()
questionnaire.questions.map {
case x: Question[String] => //...default the answer.value to some random string
case x: Question[Double] => //...default the answer.value to some random double
case x: Question[LocalDate] => //...default the answer.value to some random date
}
Run Code Online (Sandbox Code Playgroud)
但是我收到了错误:
非变量类型参数类型为pattern examplespacespace.Question [String]的字符串未选中,因为它已被擦除消除.
我不想将所有类型包装成这样的类:
case class StringQuestion(question: Question[String])
Run Code Online (Sandbox Code Playgroud)
避免此错误的最佳方法是什么?