【问题标题】:Correct way of Post(HTTP) values from entity c#来自实体 c# 的 Post(HTTP) 值的正确方式
【发布时间】:2017-05-24 03:28:17
【问题描述】:

所以,我是实体的新手。我正在尝试发布一些值,但我无法在另一个模型类中获取一个模型类的值。 如果我正在考虑做错什么,我感谢任何有关正确方法的帮助。

模型类 1(结果)

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; }

}

在我的控制器中,我收到一个值为“resultados”且值为“respLysholm”的对象,它是“resultados”类中的类。 我需要将“respLysholm”的值发送到数据库。现在,只有“resultados”值正在运行。

[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 的正文...我想获取“respostasLysholm”的所有值。

{"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}}

【问题讨论】:

    标签: c# entity-framework asp.net-web-api controller


    【解决方案1】:

    您的代码很难阅读,因为您没有遵循任何 C# 或 .NET 约定。你所拥有的是一对多的关系。以下是如何以一对多的关系将项目添加到 db。在这个例子中,我们有一个可以有很多学生的班级:

    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 值的正文进行了编辑。如果你能帮助我,我将不胜感激......谢谢
    • 我手动做的,但是太丑了...respLysholm respostas = new respLysholm { idPac = Resultados.perfilPac_idPac, resultados_idResult = resultados.idResult, questao1 = Resultados.respostasLysholm.questao1, questao2 = Resultados.respostasLysholm.questao2, };
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 2016-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-15
    相关资源
    最近更新 更多