Home EF doesn't pull foreign key when saving in controller MVC
Reply: 1

EF doesn't pull foreign key when saving in controller MVC

tmott13
1#
tmott13 Published in 2017-09-14 02:26:12Z

I've been kind of going in circles trying to get the id (foreign key) to pull from the foreign key table inside my controller method. I have tried a bunch of different methods and techniques but no luck. footer_image_det is the "child" table. I'm using a pk/fk combined but not sure if that is the best method or not. When I debug through the controller, the model isn't pulling the footer_image id, it's always 0. How do I get it to pull the correct id? I've added many images so the id is definitely not 0.

This is the error I get: The INSERT statement conflicted with the FOREIGN KEY constraint "FK_footer_image_dets_footer_images". The conflict occurred in database "rsqps", table "wst.footer_images", column 'id'. The statement has been terminated.

Model 1:

  public partial class footer_images
  {
    [Key]
    public int id { get; set; }

    [Required]
    [StringLength(200)]
    public string file_name { get; set; }

    [Required]
    [StringLength(50)]
    public string content_type { get; set; }

    [Required]
    public byte[] image { get; set; }

    public int? footer_id { get; set; }

    public virtual footer_image_dets footer_image_dets { get; set; }
}

Model 2:

  public partial class footer_image_dets
{
    [Required]
    [StringLength(150)]
    public string target_url { get; set; }

    [Key, ForeignKey("footer_images")]
    public int id { get; set; }

    public virtual footer_images footer_images { get; set; }
}

Controller method:

    public IHttpActionResult Post(footer_image_dets footer_image_detail)
    {
        if (ModelState.IsValid)
        {

            db.footer_image_dets.Add(footer_image_detail);
            db.SaveChanges();
        }           
        return CreatedAtRoute("DefaultApi", new { id = footer_image_detail.id }, footer_image_detail);           
    }
Vecchiasignora
2#
Vecchiasignora Reply to 2017-09-15 05:43:47Z

You have one-to-one relation with parent-child (by foreign key) and so i think you should insert child model via parent model, try it

public IHttpActionResult Post(footer_image_dets footer_image_detail)
    {
        if (ModelState.IsValid)
        {
            var parent = db.footer_images.Find(footer_image_detail.Id);
            if (parent != null) 
            {
              parent.footer_image_dets.Add(footer_image_detail);
              // it will work normally if you dont have child model(because  
              // your foreign key is key and there cant be inserted second  
              // row by same id, if you have child model you can update it,  
              // it depends on your logic do you want to have one-to-one 
              // reletion or no... )
              db.SaveChanges();
            }
            else 
            {
              //Create exception or yous specific logic here
            }
        }           
        return CreatedAtRoute("DefaultApi", new { id = footer_image_detail.id }, footer_image_detail);           
    }
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.310121 second(s) , Gzip On .

© 2016 Powered by cudou.com design MATCHINFO