Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update navigation properties / related tables in EF?

I have an object Customer with a navigation property Days (days is a separate table which have - day_id, customer_id - FK).

mycontext.Customers.ApplyCurrentValues(cust);
mycontext.SaveChanges();

This only updated the scalar properties of Customer, not days. Is There any smart way to update Days? (without iterating manually on days..)? if not - is there any best practice to update the second table (days)? If it's possible please write the explicit code should be used.

p.s. I'm currently using EF 4.0

like image 558
BornToCode Avatar asked Sep 11 '12 20:09

BornToCode


People also ask

What are navigation properties in Entity Framework?

A navigation property is an optional property on an entity type that allows for navigation from one end of an association to the other end. Unlike other properties, navigation properties do not carry data.

How do I update entity in EF?

To update this entity we need to attach the Department to the context and inform it to mark its status as Modified . Now if we call the SaveChanges method, the context will send an update query to the database.

What is reference navigation property?

Reference navigation property: A navigation property that holds a reference to a single related entity. Inverse navigation property: When discussing a particular navigation property, this term refers to the navigation property on the other end of the relationship.

How do I update my net core model?

Updating the Model If you need to re-scaffold the model after database schema changes have been made, you can do so by specifying the -f or --force option e.g.: dotnet ef dbcontext scaffold "Server=. \;Database=AdventureWorksLT2012;Trusted_Connection=True;" Microsoft. EntityFrameworkCore.


1 Answers

You can use GraphDiff library if you want a simple and easy way to work with disconnected object.

mycontext.UpdateGraph(cust, map => map.OwnedCollection(x => x.Days));
mycontext.SaveChanges();

This will insert or update customer object and also updating Days collection as well.

like image 200
Yuliam Chandra Avatar answered Sep 24 '22 19:09

Yuliam Chandra