繁体   English   中英

如何使用实体框架保存外键?

[英]How can I save a foreign key using Entity Framework?

首先介绍一些背景。 这是我创建的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)
);

它生成以下实体框架模型:

替代文字

在这里,我试图创建一个简单的应用程序,以便我可以学习如何使用SQLite,以便可以将新的Person保存到数据库,也可以保存新的部门。 一个部门只能有一个领导者,一个人可以是多个部门的领导者。

现在,我专注于创建一个新部门。

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

如何使用组合框保存所选“人员”的ID?

替代文字

编辑!

遵循John H.的建议,我的Department类(由EF生成)不包含Leader的定义(如果使用MS SQL,这是我所期望的)。

下面是拥有的一个屏幕截图。 再次感谢您的大力帮助。 替代文字

您需要对人员进行存根并将其附加到上下文中,并使用该存根人员作为您在Department Object中的LEADER的引用。

存根实体参考 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM