delete
delete

Reputation:

How can I save a foreign key using Entity Framework?

first some background. Here is the SQLite script I created:

create table Person(
ID integer not null primary key autoincrement,
Name text not null
);

create table Department(
ID integer not null primary key autoincrement,
Name text not null,
Leader integer not null references Person(ID)
);

It generates the following Entity Framework model:

alt text

Here, I'm trying to create make a simple application so I can learn how to use SQLite, so I can save a new Person to the database, and also save a new department. A department can have only 1 leader, and a person can be leader of many departments.

Right now I'm focusing on creating a new department.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SQLite_Testing_Grounds
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            LoadUsersToComboBox();
        }

        private void LoadUsersToComboBox()
        {
            throw new NotImplementedException();
        }

        private void button2_Click(object sender, EventArgs e)
        {            
            CreateNewPerson();
        }

        private void CreateNewPerson()
        {
            if (textBox2.Text != String.Empty)
            {
                ScansEntities1 db = new ScansEntities1();
                Person user = new Person()
                {
                    Name = textBox1.Text
                };

                db.AddToPeople(user);
                db.SaveChanges();
            }            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            CreateNewDepartment();
        }

        private void CreateNewDepartment()
        {
            if ((textBox1.Text != String.Empty) && (comboBox1.SelectedIndex >= 0))
            {
                ScansEntities1 db = new ScansEntities1();                            
                Department department = new Department()
                {
                    Name = textBox1.Text,
                    //Then what goes here? :/

                };
            }
            throw new NotImplementedException();
        }
    }
}

How can I save the ID of the selected "Person" using the ComboBox?

alt text

EDIT!

Following John H. advice, my Department class (generated by EF) doesn't contain a definition for Leader (as I would expect if I were using MS SQL).

Here is a screenshot of what it does have. Thanks again for the massive help. alt text

Upvotes: 1

Views: 9532

Answers (1)

John Hartsock
John Hartsock

Reputation: 86882

You need to stub out the person and attach it to the context use that stubbed person as reference for you LEADER in your Department Object.

Stub Entities Reference Link

    private void CreateNewDepartment()
    {
        if ((textBox1.Text != String.Empty) && (comboBox1.SelectedIndex >= 0))
        {
            ScansEntities1 db = new ScansEntities1();
            Person person = new Person() {Id = /*SomeId*/};
            db.AttachTo("Person", person);
            Department department = new Department()
            {
                Name = textBox1.Text,
                Person = person

            };
            db.AddToDepartment(department);
            db.SaveChanges();

        }
    }

Upvotes: 3

Related Questions