0

I need help understanding something. I'm using C# and Web API 2. I'm trying to Post Claim and insert in the database.

The Claim Model looks something like the following:

public class Claim
{
    public int id { get; set; }
    public int rxId { get; set; }
    public int patientId { get; set; }
    public int vitalsId { get; set; }
    public int screeningToolId { get; set; }
    public int prescriberId { get; set; }
    public int labId { get; set; }
    public int physicalId { get; set; }
    public int radiologyId { get; set; }

    
    [ForeignKey("patientId")]
    public virtual Member Member { get; set; }

    [ForeignKey("prescriberId")]
    public virtual Prescriber Prescriber { get; set; }

    [ForeignKey("rxId")]
    public virtual RxTable Rx { get; set; }

    [ForeignKey("screeningToolId")]
    public virtual ScreeningTools ScreeningTools { get; set; }

    [ForeignKey("vitalsId")]
    public Vitals Vitals { get; set; }

    [ForeignKey("labId")]
    public virtual LabResults Labs { get; set; }

    [ForeignKey("physicalId")]
    public virtual PhysicalResults Physical { get; set; }

    [ForeignKey("radiologyId")]
    public virtual Radiology Radiology { get; set; }
    
    public virtual ICollection<ClaimFileModel> ClaimFiles { get; set; }

    public virtual ICollection<MissingItems> MissingItems { get; set; }

    public virtual ICollection<ClaimResponse> Responses { get; set; }

    public Claim()
    {
        this.ClaimFiles = new HashSet<ClaimFileModel>();
        this.Responses = new HashSet<ClaimResponse>();

    }

}

The following is my controller:

    public async Task<IHttpActionResult> PostClaim(Claim claim)
    {
         claim.createdOn = DateTime.UtcNow;

        //db.ScreeningTools.Add(claim.ScreeningTools);
        //db.Physical.Add(claim.Physical);
        //db.Labs.Add(claim.Labs);
        //db.Vitals.Add(claim.Vitals);
        //db.Prescriber.Attach(claim.Prescriber);
        //db.RxTable.Attach(claim.Rx);

        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }

        db.Claim.Add(claim);
        await db.SaveChangesAsync();

        return Ok(claim);
    }

This posts to the database, but Claim table doesn't show physicalId, labsId, vitalsId, screeningToolsId, but rxId shows up. I don't understand how it's getting the id. Although, one thing to note is that claim from body already has object Rx that contains rxId. But the rest such as screeningTools, physicalId etc. doesn't contain Id because it's something I'm also trying to insert. I thought navigation property will insert the item in the database if it doesn't exist and associate the id with the model. Am I mistaken? Can anyone explain this a bit? I may have been confused on what I ready so far about navigation properties.

RAW DATA

[{
  screeningToolId: 0,
  screeningTool:{
    comm:true,
    dast:true,
    dire:true,
    ort:false,
    pduq:true,
    pmq:false,
    riskAssessment:2
   }, 
   physicalId: 0,
   physical: {
    ....
   },
   labId: 0,
   labs: {
     .... 
   }

}]

It appears something like this. The server does insert the data in the respective table, and when I get the data back, these objects have id's listed too.

13
  • Can you show the raw data you're posting over http, please? Commented Sep 30, 2016 at 22:51
  • I just added sample of the data. Notice how screeningToolId is 0. After I POST, the data seems to be inserted and I have setup to send the inserted data back as response, and I see that there's id within screeningToolId object. However, screeningToolId is still 0. How can I fix that? Commented Oct 1, 2016 at 1:46
  • 1
    to start: don't use domain model as view model! NEVER! Commented Oct 1, 2016 at 2:19
  • could you elaborate please. Commented Oct 1, 2016 at 2:20
  • @user1828605 look at this answer stackoverflow.com/questions/37727976/… (correct way section). Your controller must have his own model as parameter Commented Oct 1, 2016 at 2:22

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.