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