简体   繁体   English

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

[英]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.

相关问题 如何使用实体框架查询外键对象? - How can I query the foreign key objects using Entity Framework? 如何在Entity Framework的外键字段中添加数据? - How can I add data in foreign key field in Entity Framework? 如何使用实体框架更改外键关联? - How do I change a foreign key association using Entity Framework? 使用实体框架,如何在两个模型上添加外键以相互引用 - Using Entity Framework, how can I add a foreign key on two models to reference each other 使用Entity Framework保存具有外键约束的数据 - Using Entity Framework to save data that have foreign key constraints 使用.NET 3.5实体框架,如何保存实体? - Using the .NET 3.5 Entity Framework how can I save an entity? 实体框架正在尝试将外键表与实体保存一起保存。 如何预防呢? - Entity Framework is trying to save the Foreign key tables along with the entity save. How to prevent that? 我可以使用Entity Framework Code First数据注释创建双向外键关系吗? - Can I create a bidirectional foreign key relationship using Entity Framework Code First data annotations? 使用实体框架时,为什么不能访问外键字段的值? - Why can't I access the value(s) of foreign key field(s) when using Entity Framework? 如何在Entity Framework中创建由外键组成的复合主键? - How can I create a composite primary key consisting of foreign keys to two tables in Entity Framework?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM