简体   繁体   English

如何使用 C# 中的实体框架更新具有外键关系的数据库中的记录

[英]How to update record in database with foreign key relationship using Entity Framework in C#

I have two tables one is basicinfo and the second is department id of the basicinfo is foreign key in the department table if we want to update the record using entity framework in C# how can we do that.我有两个表,一个是基本信息,第二个是基本信息的部门 id,如果我们想使用 C# 中的实体框架更新记录,那么基本信息是部门表中的外键,我们该怎么做。 Basic Info基本信息

 public partial class basicinfo
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public basicinfo()
    {
        this.Departments = new HashSet<Department>();
    }

    public int empID { get; set; }
    public string fName { get; set; }
    public string lName { get; set; }
    public Nullable<int> Age { get; set; }

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<Department> Departments { get; set; }
}

Department table部门表

 public partial class Department
{
    public int id { get; set; }
    public int idF { get; set; }
    public string departmentName { get; set; }

    public virtual basicinfo basicinfo { get; set; }
}

Here is I am going to edit the record in the tables it works there is no error in debugging but I need to refine this because I am getting the record separately from the database I need to do this in efficient way这是我要编辑它工作的表中的记录调试没有错误但我需要改进这一点,因为我从数据库中单独获取记录我需要以有效的方式执行此操作

private void update_Click(object sender, RoutedEventArgs e)
    {
        basicinfo data = (from m in db.basicinfoes where m.empID == id select m).Single();
        Department deprtData = db.Departments.Where(m => m.idF == id).Single();
        data.fName = fname.Text;
        data.lName = lname.Text;
        data.Age = Convert.ToInt32(age.Text);
        deprtData.departmentName = departmentName.Text;
        db.SaveChanges();
        
    }

Efficiency here is related to your goals.这里的效率与您的目标有关。

Two EF queries to retrieve small pieces of data isn't considered too problematic, but if network latency is an issue, or if the possibility the data from one table could change before the second query runs, those could be problems.用于检索小块数据的两个 EF 查询不被认为有太大问题,但如果网络延迟是一个问题,或者如果一个表中的数据可能在第二个查询运行之前发生更改,那么这些可能是问题。

If you'd like to retrieve the entire entity try using Include to eagerly load the related entity:如果您想检索整个实体,请尝试使用Include急切加载相关实体:

private void update_Click(object sender, RoutedEventArgs e)
    {
        Department deprtData = db.Departments
                                   .Include(d => d.basicinfo)
                                   .Where(m => m.idF == id)
                                   .Single();
        deprtData.basicinfo.fName = fname.Text;
        deprtData.basicinfo.lName = lname.Text;
        deprtData.basicinfo.Age = Convert.ToInt32(age.Text);
        deprtData.departmentName = departmentName.Text;
        db.SaveChanges();
    }

This is one way of doing it that will eagerly load the related data and then allow you to modify it from the Department entity.这是一种执行此操作的方法,它会急切地加载相关数据,然后允许您从Department实体对其进行修改。

Just load entity, modify it, then call SaveChanges() or SaveChangesAsync() on your context object.只需加载实体,修改它,然后在您的上下文对象上调用SaveChanges()SaveChangesAsync()

If you mean "How to update foreign key bundle", either, just update loaded entity field and save.如果您的意思是“如何更新外键包”,只需更新加载的实体字段并保存。

UPD1: Use Include method UPD1:使用Include方法

var deprtData = db.Departments.Include(d => d.basicinfo)...

then:然后:

deprtData.basicinfo.fName = fname.Text;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用C#实体框架删除数据库记录? - How to delete a database record using c# entity framework? ASP.NET C#实体框架-如何正确更新外键? - ASP.NET C# Entity Framework - How to update Foreign Key properly? ASP.NET C#实体框架-如何正确更新外键? - 第2部分 - ASP.NET C# Entity Framework - How to update Foreign Key properly? - Part 2 如何使用实体框架删除外键记录? - How to delete a foreign key record with Entity Framework? 实体框架中的外键关系 - Foreign Key relationship in Entity Framework C#实体框架中的外键 - Foreign Key in Entity Framework in C# 使用实体框架更新外键 - Update foreign key using Entity Framework C# 实体框架代码优先 - 如何仅使用该外键的 id 添加带有外键的行? - C# Entity Framework Code-First- How do you add a row with foreign key using just the id of that foreign key? 使用实体框架中的数据库优先方法,如何仅从具有多个外键关系的单个表中获取数据? - Using Database first approach in Entity framework, how to fetch the data from a single table only which has multiple foreign key relationship? 如何使用实体框架c#显示与表中关联的外键的三个表的值? - how to show values of three table with foreign key associated in table using entity framework c#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM