Entity Framework Core 3.0 new features - filtered include - asp.net-core

Following this problem with filtering Include in EF Core:
stackoverflow question
is this something that will be possible in EF 3.0?

This is now possible, using .Net 5:
var blogs = context.Blogs
.Include(e => e.Posts.Where(p => p.Title.Contains("Cheese")))
.ToList();

By default, EF Core doesn't support to filter in the Include method.
Disclaimer: I'm the owner of the project Entity Framework Plus
The EF+ Query IncludeFilter (free and open source) allows easily filter included entities.
The ThenInclude is not yet supported but you just have to include the last filter to have the same behavior.
Example:
_dbContext.User
.IncludeFilter(x=>x.UserRoles)
.IncludeFilter(x=>x.UserRoles.SelectMany(y => y.Roles.Where(z => z.Active)))

Related

Is it possible to have deferred HashSet in Entity Framework Plus?

I am using EF Core in .NET 7 and I can't cache HashSet in EF Plus, so every time I need to iterating all items in that case:
var emailAlreadyExist = dbContext.Users.Select(c => c.Email).FromCache().Contains(newEmail);
Maybe is there some way how can I cache HashSet?
Something like:
var emailAlreadyExist = dbContext.Users.Select(c => c.Email).DeferredHashSet().FromCache().Contains(newEmail);

AspNet Core WebApi OData - Many to Many join tables

As is the recommended pattern in EF Core these days when you have a many to many join we do something like this:
Fluent API, many-to-many in Entity Framework Core
Having done that I'm faced with the issue as to how I go about exposing that in the OData model.
Since technically the Entity type definition has no key property (as it uses a composite key the OData framework doesn't like me adding the set to the model.
Whats the recommended approach to this problem?
It seems that EF and OData have become somewhat synced up, what would be even better would be if they could literally share model building code.
To that end I found that in the OData model build after calling AddSet I was able to define the key in the same way as I did in EF ...
Builder.EntityType<UserRole>().HasKey(ac => new { ac.UserId, ac.RoleId });
This is somewhat clean, I have not yet tried posting or directly requesting such a type yet, but expanding from either side of the relationship chain seems to work fine.
EDIT: Moredetails on the definition of this in controller ...
The Url needs to contain both key parts ...
HTTP GET ~/UserRole(123, 456)
the same with PUTs and DELETEs but POST's don't contain the key they are part of the object in the body.
The method signature must contain both key parts, here's some examples ...
public IActionResult Get([FromODataUri]int keyUserId, [FromODataUri]Guid keyRoleId)
{
...
}
public IActionResult Put([FromODataUri]int keyUserId, [FromODataUri]Guid keyRoleId)
{
...
}
public IActionResult Post(UserRole entity)
{
...
}

Ninject singleton scope loading

I am very much new to ASP.NET MVC and I need help loading some types in singleton scope using Ninject.
--- existing code looks as ----
List<Type> types = loading some types into list here.
foreach (var type in types.Where(O => O.Name.StartsWith("I")))
{
Kernel.Bind(type).To(Type.GetType(type.FullName.Replace(".I", ".")));
}
My job is to bind these types in singleton scope and I am not sure how to do that.
After kernel.Bind().To() put .InSingletonScope()
Also have a look at the Ninject conventions extension. It makes stuff like this a lot easier.
For example you could write it like this instead.
kernel.Bind(x =>
x.FromThisAssembly()
.SelectAllClasses()
.Where(types.Contains)
.BindDefaultInterface()
.Configure(b => b.InSingletonScope()));
Depending on how you get your list of types, this might be written even easier. Just check out the documentation and the samples on the wiki.
https://github.com/ninject/ninject.extensions.conventions/wiki

Mapping to custom types in NHibernate 3.2

I have just started to use NHibernate 3.2 with its new Conformist API, having used previous versions with Fluent a while back. The basic stuff seems fine but I am currently struggling with trying to map a string to a custom type.
In this specific case, I have a string which is a semi-colon separated list of roles in a column on one of my tables. When I get it out, I want it to be mapped into a "RoleSet" custom object that I have created by passing the string value from the database into its constructor.
I have created a IUserType but I cannot see how to tell it to use it.
Previously with Fluent I would have done this in my map class:
Map(x => x.Roles).CustomType<RoleSetType>();
Is there an equivalent way to do this is in the new API?
Give this a try...
Property(x => x.Roles, x => x.Type(typeof(RoleSetType), null));

Unable to figure out how to do Joins within IQueryable

Here is what I am trying:
IQueryable query = this.MyRepository.GetShippingCollection();
IList<SomeListType> myList = query.Where(x => x.Settings
.Where(y => y.SelectorID.Equals(5))
.Count() > 0)
.OrderBy(x => x.Order)
.ToList();
Produces this error:
could not resolve property: Settings.ID
If I do it this way it works, but causes over 3,000 queries on my SQL Server:
IList<SomeListType> myList = this.MyRepository.GetShippingCollection().ToList();
myList = myList.Where(x => x.Settings
.Where(y => y.SelectorID.Equals(5))
.Count() > 0)
.OrderBy(x => x.Order)
.ToList();
I know the solution resides within using a "Join".
I have been looking at examples for the last couple hours and can only find Join examples within the Mapping file. I am also finding examples for "ICriteria".
I don't want to have to create seporate entries for all my complex queries in the mapping file so the join within that file will not work.
Since I am using Fluent NHibernate, I am not using "ICriteria". I am using "IQueryable". How do you join multiple tables within "IQueryable"?
Any help with this would be greatly appreciated.
Thanks in advance.
If the second query is executing 3,000 queries, it is almost certainly lazy-loading the Settings collection. As you iterate over the list, you access this collection, and each time NHibernate goes back to the database to fetch it. Try setting the fetch mode for the Settings property to eager load in the mapping.
Beyond that, the LINQ provider could be an issue. What version of NHibernate are you using? The 2.x LINQ provider has some real limitations. It has been reimplemented in the 3.0 trunk, but you'll have to download and compile it from the source.
By the way, ICriteria vs IQueryable is not related to Fluent NHibernate. Criteria API and LINQ are two providers through which you can create queries. Fluent NHibernate is an alternative way to perform configuration.