Hi guys, 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:
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?
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.