EF Code First + lazy loading, projection does not work as expected - entity-framework

I have something like this:
var tmp =_forumsDb.Threads
.Where(t => t.Id == variable)
.Select(t => new { Thread = t, Posts = t.Posts.Take(1) })
.Single();
Now, i expect tmp.Thread.Posts.Count(); to be 1, but it takes all posts that i have in database. Is it possible to use projection that takes explicit amount of posts, do it in a single query without turning off lazy loading?
Edit:
I tried doing something like this, but it does not work either:
var tmp =_forumsDb.Threads
.Where(t => t.Id == variable)
.Select(t => new { Thread = t, Posts = t.Posts.OrderBy(p => p.DateCreated).Take(1) })
.Select(t => t.Thread)
.Single();

tmp.Thread.Posts is the navigation property for which lazy loading is configured. Since it isn't yet loaded, accessing it loads all the remaining posts.
tmp.Posts is not a navigation property. That's the one you should be able to access without triggering another query.

Related

EF Core entity refresh

I would like if there is a way to trigger an object hierachy's lazy load after it has been added in the cache following an insert.
If I insert an object only providing it the foreign keys ids and try to request it right after, none of these properties / collections are retrieved.
Example:
_dbContext.Set<MYTYPE>().Add(myObject);
await _dbContext.SaveChangesAsync(ct);
_dbContext.Entry(myObject).Collection(c => c.SomeCollection).Load();
_dbContext.Entry(myObject).Reference(c => c.Property1).Load();
_dbContext.Entry(myObject.Property1).Reference(c => c.NestedProperty).Load();
_dbContext.Entry(myObject).Reference(c => c.Property2).Load();
_dbContext.Entry(myObject).Collection(c => c.SomeCollection2).Load();
_dbContext.Entry(myObject).Reference(c => c.Property3).Load();
_dbContext.Entry(myObject).Reference(c => c.Property4).Load();
_dbContext.Entry(myObject.Property4).Reference(c => c.SomeOtherNestedProperty).Load();
_dbContext.Entry(myObject).Reference(c => c.Property5).Load();
_dbContext.Entry(myObject).Reference(c => c.Property6).Load();
return _dbContext.Set<MYTYPE>().Where(x => x.Id == myObject.Id);
Unless I do all the Load() (having to nest into several layers sometimes ...), I can't have any of the sub properties properly lazy loaded.
Note: doing
_dbContext.Entry(myObject).Reload()
or
_dbContext.Entry(myObject).State = EntityState.Detached;
doesn't work.
Any idea? Because I have several cases like this, with HEAVY object nesting, and it feels really bad to have to do all the loads manually.
Thanks.
The thing is, if you try to add some line using only the integer as a foreign key like below :
db.tablename.Add(new tablename{
name = "hello",
FK_IdofanotherTable = 5
});
db.SaveChanges();
Then entity does not care about linking the object immediately, you won't be able to access the linked object as a property.
This is how it's actually done in modern Entity Framework :
var TheActualCompleteObject = db.AnotherTable.First(t => t.id == 5);
db.tablename.Add(new tablename{
name = "hello",
AnotherTable = TheActualCompleteObject
});
db.SaveChanges();
So... basically, you will have loaded the object anyway, it's not gonna make anything faster, but it will be more consistant. You will be able to navigate through foreign keys properties.
If this is not possible, then you will have to query back AFTER SaveChanges to retrieve your object AND have access to foreign properties (eagerly loading them, of course, or else it's the same disaster)
Exemple :
_dbContext.Configuration.LazyLoadingEnabled = false;
_dbContext.Set<MYTYPE>().Add(myObject);
await _dbContext.SaveChangesAsync(ct);
var v = _dbContext.First(t => t.id == myObject.id);
var fk_prop = v.Property1;
I also found no easy way around this.
The following might make it simpler...
public static void Reset(this DbContext context)
{
var entries = context.ChangeTracker
.Entries()
.Where(e => e.State != EntityState.Unchanged)
.ToArray();
foreach (var entry in entries)
{
switch (entry.State)
{
case EntityState.Modified:
entry.State = EntityState.Unchanged;
break;
case EntityState.Added:
entry.State = EntityState.Detached;
break;
case EntityState.Deleted:
entry.Reload();
break;
}
}
}
Hope this helps!
JS

EF DbContext. How to avoid caching?

Spent a lot of time, but still cann't understand how to avoid caching in DbContext.
I attached below entity model of some easy case to demonstrate what I mean.
The problem is that dbcontext caching results. For example, I have next code for querying data from my database:
using (TestContext ctx = new TestContext())
{
var res = (from b in ctx.Buildings.Where(x => x.ID == 1)
select new
{
b,
flats = from f in b.Flats
select new
{
f,
people = from p in f.People
where p.Archived == false
select p
}
}).AsEnumerable().Select(x => x.b).Single();
}
In this case, everything is fine: I got what I want (Only persons with Archived == false).
But if I add another query after it, for example, query for buildings that have people that have Archived flag set to true, I have next things, that I really cann't understand:
my previous result, that is res, will be added by data (there
will be added Persons with Archived == true too)
new result will contain absolutely all Person's, no matter what Archived equals
the code of this query is next:
using (TestContext ctx = new TestContext())
{
var res = (from b in ctx.Buildings.Where(x => x.ID == 1)
select new
{
b,
flats = from f in b.Flats
select new
{
f,
people = from p in f.People
where p.Archived == false
select p
}
}).AsEnumerable().Select(x => x.b).Single();
var newResult = (from b in ctx.Buildings.Where(x => x.ID == 1)
select new
{
b,
flats = from f in b.Flats
select new
{
f,
people = from p in f.People
where p.Archived == true
select p
}
}).AsEnumerable().Select(x => x.b).Single();
}
By the way, I set LazyLoadingEnabled to false in constructor of TestContext.
Does anybody know how to workaround this problem? How can I have in my query what I really write in my linq to entity?
P.S. #Ladislav may be you can help?
You can use the AsNoTracking method on your query.
var res = (from b in ctx.Buildings.Where(x => x.ID == 1)
select new
{
b,
flats = from f in b.Flats
select new
{
f,
people = from p in f.People
where p.Archived == false
select p
}
}).AsNoTracking().AsEnumerabe().Select(x => x.b).Single();
I also want to note that your AsEnumerable is probably doing more harm than good. If you remove it, the Select(x => x.b) will be translated to SQL. As is, you are selecting everything, then throwing away everything but x.b in memory.
have you tried something like:
ctx.Persons.Where(x => x.Flat.Building.Id == 1 && x.Archived == false);
===== EDIT =====
In this case I think you approach is, imho, really hazardous. Indeed you works on the data loaded by EF to interpret your query rather than on data resulting of the interpretation of your query. If one day EF changes is loading policy (for example with a predictive pre-loading) your approach will "send you in then wall".
For your goal, you will have to eager load the data you need to build your "filterd" entity. That is select the building, then foreach Flat select the non archived persons.
Another solution is to use too separate contexts in an "UnitOfWork" like design.

RX PropertyChanged GroupBy deadlock

I am trying to use Reactive Extensions to throttle PropertyChanged notifications. There are examples of doing this using GroupBy, but with one Subscription created for each PropertyName.
I want to handle the PropertyChanged event for all properties, and I need to Throttle those events for each PropertyName.
This is what I have so far, but it causes a deadlock.
ValuesPropertyChanged = Observable.FromEventPattern<PropertyChangedEventArgs>(value, "PropertyChanged")
.GroupBy(o => o.EventArgs.PropertyName)
.First()
.Throttle(TimeSpan.FromSeconds(2))
.Subscribe(args => HandlePropertyChanged(args.EventArgs.PropertyName));
The deadlock happens in the call to .First().
It still locks if I change that line to:
.Select(o => o.First())
I have also tried
.Select(o => o.FirstAsync())
The examples for GroupBy here look pretty concise, but I am incapable of wrapping my head around converting these examples to my solution.
Why does this cause a deadlock, and what should I do to make this work?
I think this might be what you're after:
// assume MyObj : INotifyPropertyChanged, naturally
var value = new MyObj();
Action<string> HandlePropertyChanged =
name => Console.WriteLine("Got a change for name:" + name);
// The query
var valuesPropertyChanged =
// create from event stream
from propChange in Observable.FromEventPattern<PropertyChangedEventArgs>(
value,
"PropertyChanged")
// group events by property name
group propChange by propChange.EventArgs.PropertyName into batchByName
// Throttle the resulting batch
from throttledByName in batchByName.Throttle(TimeSpan.FromSeconds(1))
// then select each item of the "throttled output"
select throttledByName;
valuesPropertyChanged.Subscribe(args =>
HandlePropertyChanged(args.EventArgs.PropertyName));
for(int i=0;i<10;i++)
{
value.Value1 = i.ToString();
value.Value2 = (i-1).ToString();
}
Output:
Got a change for name:Value2
Got a change for name:Value1
Here is the same but with extension methods:
var valuesPropertyChanged =
Observable.FromEventPattern<PropertyChangedEventArgs>(
_vm,
"PropertyChanged")
.GroupBy(propchange => propchange.EventArgs.PropertyName)
.Select(o => o.Throttle(TimeSpan.FromSeconds(1)))
.Merge();

Use Take to including entity

I have two tables: categories and articles. One category can have many articles. I want to display overview of new articles separated by categories. But I want to display only first five articles per each category. How should I write query to solve this?
My first idea was something like this:
var cat = from c in ctx.categories
where c.IsPublic == true
select c;
But this contains all articles and I'm not sure how to write something like c.articles.Take(5) to query. Is it possible?
Oh, and it is ASP.NET MVC 2 with Entity Framework.
This worked for me. Important here is to switch off lazy loading to avoid that all articles are loaded when you iterate through the collection:
ctx.ContextOptions.LazyLoadingEnabled = false; // assuming you are in EF 4.0
var query = ctx.categories
.Where(c => c.IsPublic)
.Select(c => new {
Category = c,
Articles = c.Articles.Take(5)
}).ToList().Select(x => x.Category);
foreach (var category in query)
{
foreach (var article in category.Articles)
{
// runs only 5 times through this loop
}
}
Edit
If you don't dispose ctx and work with the context more after the code snippet above you probably better reenable lazy loading again ...
ctx.ContextOptions.LazyLoadingEnabled = true;
... before you get into trouble with other queries if your application mostly relies on lazy loading.
ctx.categories
.Where(c => c.IsPublic)
.Select(c => new
{
Category = c,
Articles = c.Articles.OrderBy(a => a.Whatever).Take(5)
).Select(c => c.Category);
Supposing that your class name is Category with an EntityCollection called Articles, this should work.

Entity Framework - Many to Many Subquery

I asked a question about this previously but my database structure has changed, and while it made other things simpler, now this part is more complicated. Here is the previous question.
At the time, my EF Context had a UsersProjects object because there were other properties. Now that I've simplified that table, it is just the keys, so all my EF context knows about is Users and Projects and the M2M relationship between them. There is no more UsersProjects as far as EF knows.
So my goal is to say "show me all the users who are working on projects with me."
in SQL, this would go something like:
SELECT * FROM Users INNER JOIN UsersProjects ON Users.ID=UsersProjects.UserID
WHERE ProjectID IN (SELECT ProjectID FROM UsersProjects WHERE UserID=#UserID)
and I started in EF with something like this:
var myProjects =
(from p in edmx.Projects
where p.Users.Contains(edmx.Users.FirstOrDefault(u => u.Email == UserEmail))
orderby p.Name
select p).ToList();
var associatedUsers =
(from u in edmx.Users
where myProjects.Contains(?????????)
//where myProjects.Any(????????)
select u);
The trick is finding what to put in the ????????. Anyone help here?
var me = context
.Users
.First(user => user.Email = "me#example.com");
// Note that there is no call to ToList() or AsEnumerable().
var myProjects = context
.Projects
.Where(project => project.Users.Contains(me));
var associatedUsers = context
.Users
.Where(user => myProjects.Any(project => user.Project.Contains(project)));
But there are several other possible solutions. For example
var associatedUsers = myProjects
.SelectMany(project => project.Users)
.Distinct();
which I would prefer.
Further note that it is much easier to obtain myProjects using a navigation property instead of using Contains().
var myProjects = me.Projects;
Daniel, I tried what you had and ran into some issues. Can you explain what these errors mean?
I tried:
// Doesn't work.
using (var edmx = new MayflyEntities())
{
var me = edmx.Users.First(user => user.Email == UserEmail);
var myProjects = edmx.Projects.Where(project => project.Users.Contains(me));
var associatedUsers = myProjects.SelectMany(project => project.Users).Distinct();
}
but got the two following exceptions:
Unable to create a constant value of type 'DomainModel.User'. Only primitive types ('such as Int32, String, and Guid') are supported in this context.
and
The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.
So, I moved some things around and this works fine, but now I'm curious as to why? In SQL Profiler, it all executes in one query, so why does it show that the context has been disposed? Also, why can it not use the me object instead of the lambda?
// Works fine
var edmx = new MayflyEntities();
var myProjects = edmx.Projects.Where(project => project.Users.Contains(edmx.Users.First(user => user.Email == UserEmail)));
var associatedUsers = myProjects.SelectMany(project => project.Users).Distinct();