[英]How can I save a foreign key using Entity Framework?
first some background. 首先介绍一些背景。 Here is the SQLite script I created: 这是我创建的SQLite脚本:
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. 在这里,我试图创建一个简单的应用程序,以便我可以学习如何使用SQLite,以便可以将新的Person保存到数据库,也可以保存新的部门。 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? 如何使用组合框保存所选“人员”的ID?
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). 遵循John H.的建议,我的Department类(由EF生成)不包含Leader的定义(如果使用MS SQL,这是我所期望的)。
Here is a screenshot of what it does have. 下面是它拥有的一个屏幕截图。 Thanks again for the massive help. 再次感谢您的大力帮助。
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. 您需要对人员进行存根并将其附加到上下文中,并使用该存根人员作为您在Department Object中的LEADER的引用。
Stub Entities Reference http://blogs.msdn.com/b/alexj/archive/2009/06/19/tip-26-how-to-avoid-database-queries-using-stub-entities.aspx 存根实体参考 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();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.