I
I
iluxa18102016-04-02 17:03:32
.NET
iluxa1810, 2016-04-02 17:03:32

Entity Framework and contexts?

If I received an object from one context, then I pass it to the method, where I open the same context.
I make changes to the object and do SaveChanges, will the data be saved in the database or should I do SaveChanges in the context where I selected the object from?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Roman, 2016-04-03
@yarosroman

Example:

var context1=new MyContext();
var context2=new MyContext();
var entity1=context1.Entities1.First();
entity1.Field1=5;
context2.SaveChanges();

The data will not be saved. Each context has its own Changes Tracker (change tracker), and based on it makes changes to the database.
in this case there are 2 options, dependency injection, and pass the context down the call chain, or
context2.Entry(entity1).State = EntityState.Modified;
context2.SaveChanges();

E
Evgeny Sotsky, 2016-04-02
@jekakmail

This is simply checked

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question