简体   繁体   中英

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. 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.

If you'd like to retrieve the entire entity try using Include to eagerly load the related entity:

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.

Just load entity, modify it, then call SaveChanges() or SaveChangesAsync() on your context object.

If you mean "How to update foreign key bundle", either, just update loaded entity field and save.

UPD1: Use Include method

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

then:

deprtData.basicinfo.fName = fname.Text;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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