How to Retrieve the data in one-ManyRelation

by Tayyab Bhatti   Last Updated February 25, 2018 12:05 PM

Entity Classes

public class Advertisement
{
    public Advertisement()
    {
        Images = new List<AdvertisementImages>();
    }
    public int ID { get; set; }

    [Required(ErrorMessage = "Please Enter the Title")]
    [Column(TypeName = "varchar")]
    [MaxLength(300)]
    public string Title { get; set; }



    public ICollection<AdvertisementImages> Images { get; set; }

}

   public class AdvertisementImages
{
    public int ID { get; set; }

    [Required]
    [Column(TypeName = "varchar")]
    [MaxLength(300)]
    public string Url { get; set; }



    [Column(TypeName = "varchar")]
    [MaxLength(100)]
    public string Caption { get; set; }

    public int Priority { get; set; }


}

And the Error is... System.Data. Entity. Infrastructure. DbUpdateException: 'An error occurred while saving entities that do not expose foreign key properties for their relationships. The Entity Entries property will return null because a single entity cannot be identified as the source of the exception. Handling of exceptions while saving can be made

Please Solve this

And The Controller Method is

 public ActionResult DeteleAdv(int id)
    {
        DemoContext _context = new DemoContext();
        Advertisement objAdv = _context.Advertisements.Find(id);
        _context.Advertisements.Remove(objAdv);
        _context.SaveChanges();

        return RedirectToAction("UserProfile", "User");
    }


Related Questions