Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6807879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:53:12+00:00 2026-05-26T19:53:12+00:00

I have 5 entity and use the custom model for the three of them

  • 0

I have 5 entity and use the custom model for the three of them but I need the foreign key of other 2 in the model the custom model are as

public class CourierRape
{
    public Sender Sender { get; set; }
    public Receiver Receiver { get; set; }
    public Parcel Parcel { get; set; }
}

The two remaining entities are Country and City

The controller is as follows

public ActionResult Create()
{
        ViewBag.S_City_Id = new SelectList(db.Cities, "Id", "Name");
        ViewBag.S_Country_Id = new SelectList(db.Countries, "Id", "Name");
        ViewBag.R_City_Id = new SelectList(db.Cities, "Id", "Name");
        ViewBag.R_Country_Id = new SelectList(db.Countries, "Id", "Name");

        return View();
} 

//
// POST: /Courier/Create
[HttpPost]
public ActionResult Create(CourierRape c)
{
        ViewBag.S_City_Id = new SelectList(db.Cities, "Id", "Name", c.Sender.S_City_Id);
        ViewBag.S_Country_Id = new SelectList(db.Countries, "Id", "Name", c.Sender.S_Country_Id);
        ViewBag.R_City_Id = new SelectList(db.Cities, "Id", "Name", c.Receiver.R_City_Id);
        ViewBag.R_Country_Id = new SelectList(db.Countries, "Id", "Name", c.Receiver.R_Country_Id);

        try
        {
            SenderController s = new SenderController();
            ReceiverController r = new ReceiverController();
            ParcelController p = new ParcelController();

            r.Create(c.Receiver);
            s.Create(c.Sender);
            c.Parcel.Sender_Id = c.Sender.Id;
            c.Parcel.Receiver_Id = c.Receiver.Id;
            p.Create(c.Parcel);

            return RedirectToAction("Index");
        }
        catch
        {
            ViewBag.Message = c.Sender.S_City_Id.ToString() +" " + c.Receiver.R_City_Id.ToString() + " " + c.Receiver.R_Country_Id.ToString()  + " "+ c.Sender.S_Country_Id.ToString();
            return View(c);
        }

but it throws the exception and return the model with the 0 of the foreign keys

And when I remove the try catch it gives the following error

The INSERT statement conflicted with the FOREIGN KEY constraint “FK_Receiver_Country”. The conflict occurred in database “452171974654368803C496909AE805A3_ENTS\VISUAL STUDIO 2010\PROJECTS\COPY OF ECOMMERCECOURIER\ECOMMERCECOURIER\APP_DATA\COURIER.MDF”, table “dbo.Country”, column ‘Id’.
The statement has been terminated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T19:53:13+00:00Added an answer on May 26, 2026 at 7:53 pm

    Look at the CountryId on the objects you’re creating and make sure they exist in your country table.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have several Entity Framework Code First DbContext objects that use a custom Initializer.
I have a model like this: public class Instrument { public string Id {
We use Entity Framework 5, but have a requirement to ALSO use a normal
In my Custom ObjectContext class I have my entity collections exposed as IObjectSet so
I have an custom class for an Core Data entity, called 'Friends'. As I
I'm using Entity Framework and I have a custum IQueryProvider. I use the Execute
I use Entity Framework 4 and I have parent - child relation with Cascade
I have an entity Product which has relationship category, which is optional, and use
I have converted an Entity framework project to use POCO objects by removing the
I use ADO.NET Entity-Framework, I have a page that is data-bound to an entity

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.