views:

30

answers:

1

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:

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

+1  A: 

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 http://blogs.msdn.com/b/alexj/archive/2009/06/19/tip-26-how-to-avoid-database-queries-using-stub-entities.aspx

    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();

        }
    }
John Hartsock
Hello, please see edit. Department class doesn't have a definition for Leader attribute. It only has ID, Name, Person, PersonReference. This is the basis of my confusion, thanks for the help! :D
Serg
@Sergio sorry I corrected my Answer
John Hartsock
I'm trying to load the combobox with People from the DB, but it doesn't seem to work: ScansEntities3 db = new ScansEntities3(); var people = db.People; comboBox1.DataSource = people; Any suggestions?
Serg