C# 从实体c获取Post(HTTP)值的正确方法#
所以,我是实体的新手。我试图发布一些值,但无法在另一个模型类中获取一个模型类的值。 如果我想做错事,我感谢任何关于正确方法的帮助 模型类1(resultados) 在我的控制器中,我接收一个值为“resultados”和值为“respLysholm”的对象,该对象是类“resultados”中的类。 我需要将“respLysholm”的值发送到数据库。现在,只剩下“resultados”值了C# 从实体c获取Post(HTTP)值的正确方法#,c#,entity-framework,asp.net-web-api,controller,C#,Entity Framework,Asp.net Web Api,Controller,所以,我是实体的新手。我试图发布一些值,但无法在另一个模型类中获取一个模型类的值。 如果我想做错事,我感谢任何关于正确方法的帮助 模型类1(resultados) 在我的控制器中,我接收一个值为“resultados”和值为“respLysholm”的对象,该对象是类“resultados”中的类。 我需要将“respLysholm”的值发送到数据库。现在,只剩下“resultados”值了 [HttpPost] [ResponseType(typeof(resultados))] public
[HttpPost]
[ResponseType(typeof(resultados))]
public async Task<IHttpActionResult> Postresultados([FromBody]resultados Resultados)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
db.resultados.Add(Resultados);
await db.SaveChangesAsync();
respLysholm respostas = new respLysholm
{
idPac = Resultados.perfilPac_idPac,
resultados_idResult = Resultados.idResult
};
\\this code doesnt work but is what i mean that i need.
\\db.respLysholm.Add(Resultados.respLysholm);
\\await db.SaveChangesAsync();
}
您的代码很难阅读,因为您没有遵循任何C#或.NET约定。你拥有的是一对多的关系。下面是如何在一对多关系中将项添加到数据库中。在本例中,我们有一个可以有许多学生的班级:
var science = new Class();
science.Name = "Sci-101";
science.Students.Add(new Student { Name = "Tom" });
science.Students.Add(new Student { Name = "Jerry" });
using (var dbCtx = new SchoolDBEntities())
{
// Add the class
dbCtx.Classes.Add(science);
// Save whole entity graph to the database.
// You do not need to save the students separately since they are part
// of the class. We have already added them to the class so saving the
// class will save the students added to the class as well.
dbCtx.SaveChanges();
}
所有这些代码都是由VS2017自动生成的。我创建了数据库,然后应用了数据库中的实体。就这个。但是代码只是在一个表中进行Post…我需要在多个表中进行Post…因此,我正在考虑“提取”值并添加到相应的数据库中。我使用Post值的主体进行了编辑。如果你能帮助我,我将不胜感激。。。thanksI是手工做的,但是太难看了<代码>respLysholm respostas=new respLysholm{idPac=Resultados.perfilPac_idPac,Resultados_idResult=Resultados.idResult,questao1=Resultados.respostasLysholm.questao1,questao2=Resultados.respostasLysholm.questao2,}代码>
[HttpPost]
[ResponseType(typeof(resultados))]
public async Task<IHttpActionResult> Postresultados([FromBody]resultados Resultados)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
db.resultados.Add(Resultados);
await db.SaveChangesAsync();
respLysholm respostas = new respLysholm
{
idPac = Resultados.perfilPac_idPac,
resultados_idResult = Resultados.idResult
};
\\this code doesnt work but is what i mean that i need.
\\db.respLysholm.Add(Resultados.respLysholm);
\\await db.SaveChangesAsync();
}
{"idResult":0,"perfilPac_idPac":1,"resultado":14,"id_questionario":1,"dtRegistro":"2017-05-23T23:28:24.916866-03:00",
"respostasLysholm":{"idPac":1,"resultados_idResult":0 ,"questao1":3}}
var science = new Class();
science.Name = "Sci-101";
science.Students.Add(new Student { Name = "Tom" });
science.Students.Add(new Student { Name = "Jerry" });
using (var dbCtx = new SchoolDBEntities())
{
// Add the class
dbCtx.Classes.Add(science);
// Save whole entity graph to the database.
// You do not need to save the students separately since they are part
// of the class. We have already added them to the class so saving the
// class will save the students added to the class as well.
dbCtx.SaveChanges();
}