【问题标题】:How can I save a foreign key using Entity Framework?如何使用实体框架保存外键?
【发布时间】:2011-05-01 21:37:20
【问题描述】:

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

如何使用 ComboBox 保存所选“人员”的 ID?

编辑!

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

这是它所做功能的屏幕截图。再次感谢您的大力帮助。

【问题讨论】:

  • 您使用的是哪个版本的 Entity Framework 和 Visual Studio?
  • Visual Studio 2010,.NET Framework 3.5,所以我猜是 EF 3.5?

标签: c# entity-framework sqlite


【解决方案1】:

您需要删除该人员并将其附加到上下文中,使用该已删除人员作为您部门对象中您的 LEADER 的参考。

Stub Entities ReferenceLink

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

        }
    }

【讨论】:

  • 您好,请查看编辑。部门类没有领导者属性的定义。它只有 ID、Name、Person、PersonReference。这是我困惑的基础,感谢您的帮助! :D
  • 我正在尝试使用数据库中的人员加载组合框,但它似乎不起作用:ScansEntities3 db = new ScansEntities3(); var people = db.People; comboBox1.DataSource = 人;有什么建议吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多