user1696698
user1696698

Reputation: 219

Getting INSERT statement conflicted with the FOREIGN KEY on SaveChanges()

I am new to working with the Entity Framework with SQL Server and I am having trouble inserting a record into my database. The problem is with 3 tables that are linked with primary keys and a foreign key and in the past I have not used databases with foreign keys.

I am just trying to get some test data into the database for now but I keep getting the error:

INSERT statement conflicted with the FOREIGN KEY

Can anyone help me get started with inserting into my database please? Here is the code I am using:

private void button3_Click(object sender, EventArgs e)
{
    TaxiDBEntities db = new TaxiDBEntities(); //connection to database
    DriverStatus newDriverStatus = new DriverStatus(); // driver status object

    /*populate the new driver object*/
    //newDriverStatus.DriverID = Convert.ToInt32(driverComboBox.Text);
    //newDriverStatus.Status = statusTextBox.Text;
    //newDriverStatus.Area = areaTextBox.Text;

    Driver driver = new Driver();

    int driverIDInt = Convert.ToInt32(driverComboBox.Text);

    //driver = db.Drivers.SingleOrDefault(p => p.DriverId == driverIDInt);
    //if (driver == null)
    //{
        driver.DriverId = driverIDInt;
        driver.Fname = "test";
        driver.Lname = "test";
    //}

    db.DriverStatus.AddObject(newDriverStatus); // add driver object to entity model

    DriverLog driverLog = new DriverLog();

    driverLog.driverLogID = driverIDInt;
    driverLog.DriverID = driverIDInt;
    driverLog.date = DateTime.Now;

    db.DriverLog.AddObject(driverLog);

    DriverStatus driverStatus = new DriverStatus();

    driverStatus.DriverID = driverIDInt;
    driverStatus.Status = "1";
    driverStatus.Area = "1";

    db.DriverStatus.AddObject(driverStatus);

    try
    {
        db.SaveChanges();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }
}

Upvotes: 0

Views: 1085

Answers (3)

Henk Holterman
Henk Holterman

Reputation: 273219

1) You're getting your ID from a textbox, int driverIDInt = Convert.ToInt32(driverComboBox.Text). That is not the best solution, consider making it an IDENTITY column.

2) Your model indoicates that Drive-DriverLog is 1-n but your code won't allow more than 1 Log:

driverLog.driverLogID = driverIDInt;
driverLog.DriverID = driverIDInt;

3) You are adding newDriverStatus to the Db but there are no keys set.

4) If you intended to link newDriverStatus to driver then adding driverStatus as well presents a conflict.

And, like others already noted, you forget to add driver to the db.

In summary, it is much easier to define your columns as INDENTITY and let the fx handle the foreign keys. Just use newDriverStatus.Driver = driver; and driverLog.Driver = driver;

Upvotes: 1

Fisch
Fisch

Reputation: 3815

You need to insert the driver into the database before you can add a status or a log. The first thing you are doing after populating you driver object is adding a status

Upvotes: 1

Andrei
Andrei

Reputation: 56688

Actual insert of the driver object into the database seems to be missing. After the line

driver.notes = "test";

you should have something like

db.Driver.AddObject(driver);

And then go on with creation of driverLog and driverStatus objects.

Upvotes: 2

Related Questions