I
I
Ilya2014-05-13 06:56:12
C++ / C#
Ilya, 2014-05-13 06:56:12

Entity Framework - how to update only one entity?

I have a certain model from the Entity Framework. It is necessary to somehow update only 1 entity (that is, fill 1 table with data from the database). For example , if you changed something in the database outside the program and only in one table, how can I update only 1 table?
I'm doing so far by simply recreating all the content:
_dataContext = new MyMagnEntities();
Because I haven't found another way yet. How to do it right?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
C
chydaya, 2014-05-13
@chydaya

It is necessary to somehow update only 1 entity (that is, fill 1 table with data from the database). For example, they changed something in the database outside the program and only in one table, how can I update only 1 table

I don’t know how Entity works, but from experience in more adequate ORMs, if something is changed in the database, then when accessing the entity that describes the table, the data will be up-to-date and without re-creation ..

A
AlexP11223, 2014-05-13
@AlexP11223

It could be something like this:

ObjectContext objectContext = ((IObjectContextAdapter)_dbContext).ObjectContext;

objectContext.Refresh(RefreshMode.StoreWins, _dbContext.SomeTable);

but not the fact that it is better than context re-creation. It seems that this does not work in all cases, for example, in my opinion, it will not delete a record deleted from the outside.
If you are loading data from that table for reading only, without editing, then use AsNoTracking()
blog.staticvoid.co.nz/2012/4/2/entity_framework_an...
blogs.msdn.com/b/adonet/archive/2011 /02/05/using-d...
then the table will just be loaded from the database every time.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question