所以,我是实体的新手。我试图发布一些值,但我无法获得另一个模型类的值。 如果我正在考虑做错事,我感谢任何有关正确方法的帮助。
模型类1(resultados)
public partial class resultados
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public resultados()
{
this.respLysholm = new HashSet<respLysholm>();
}
public int idResult { get; set; }
public int perfilPac_idPac { get; set; }
public int resultado { get; set; }
public int id_questionario { get; set; }
public System.DateTime dtRegistro { get; set; }
//public respLysholm respostasLysholm { get; set; }
public virtual perfilPac perfilPac { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<respLysholm> respLysholm { get; set; }
}
模型类2(respLysholm):
public partial class respLysholm
{
public int idPac { get; set; }
public int resultados_idResult { get; set; }
public int questao1 { get; set; }
public int questao2 { get; set; }
public int questao3 { get; set; }
public int questao4 { get; set; }
public int questao5 { get; set; }
public int questao6 { get; set; }
public int questao7 { get; set; }
public int questao8 { get; set; }
public System.DateTime dtRegistro { get; set; }
}
在我的控制器中,我收到一个值为&#34; resultados&#34;的对象。和#34; respLysholm&#34;的价值观这是课堂内的课程#34; resultados&#34;。 我需要发送&#34; respLysholm&#34;的值。到数据库。现在,只是&#34; resultados&#34;价值观正在发生。
[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();
}
Post的主体......我希望获得&#34; respostasLysholm&#34;的所有价值。
{"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}}
答案 0 :(得分:0)
您的代码很难阅读,因为您没有遵循任何C#或.NET约定。你拥有的是一对多的关系。以下是如何以一对多关系向db添加项目的方法。在这个例子中,我们有一个可以有很多学生的班级:
path=%path%;C:\Sq\MSBuild.SonarQube.Runner-1.0.1
MSBuild.SonarQube.Runner begin /n:Backlogmaps /v:1.0 /k:blm
Msbuild
MSBuild.SonarQube.Runner end