NHibernate query mapped collection - linq

In my ASP.NET web-application I use NHibernate to persist my "User"-Instances, where each of them has a "Entries" - collection. It is a typical one-to-many mapping and it works just fine. The mapping-code for the entries looks like this:
<bag name="Entries" cascade="all-delete-orphan">
<key column="UserID" />
<one-to-many class="MyApp.Entities.Entry, MyApp.Entities" />
</bag>
Now I have a page, where I want to display a grid with all the entries of the logged-in user. To do so, I could simply bind the "Entries" property of the current user to the Grids "DataSource" - property. This also works just fine, but this also means, that the grids built-in paging-functionality (Telerik RadGrid) doesn't have any effect on database-performance, because all the entries will be loaded each time when displaying the grid.
Therefore I could apply my custom-paging, where I only fetch the rows which I need to display the grids current page. A typical Linq2NHibernate query looks like this:
var query = from entry in Session.Linq<Entry>()
where entry.User == currentUser
select entry;
query.Skip(pageNum * pageSize).Take(pageSize).ToList();
Using this approach I need to extend my repository altough NHibernate has already done the mapping between User and Entry...
My question is: If I use LINQ to directly query the "Entries"-collection of my "User"-object - does this mean, that all the Entries will be loaded from the database and then filtered in memory or would this be translated to a real "database"-query, so that I could use this much more comfortable approach to implement paging?
Example:
myGrid.DataSource = currentUser.Entries.Skip(pageNum * pageSize).Take(pageSize).ToList();
J4I: Of course I use lazy-loading in the mapping files...
Thank you in advance!

LINQ on the collection will always be LINQ-to-objects, as they don't implement IQueryable, so you'd be loading everything in memory.
A query is the only possible approach at this moment.

Related

Using both basic AND association mapping for one field with Doctrine?

I have a Variable object which may belong to a group
I'm mapping the Variable field containing the FK to the Group's id and his relation association in my domain object. (using XMl).
In other words, my domain object has both a (string) field with the id and a "relation" field with the object.
The intended goal here is to retrieve a ready Group object when querying, while simply setting an id when writing
So when I create/update a Variable, I set the ID (not the relation Object).
My issue is : the value is (always) properly saved when I UPDATE, but (always) lost on INSERT.
Here's the part of my mapping :
<field name="parentId" type="string" length="36" column="parent" nullable="true"/>
<many-to-one field="parent" target-entity="App\Domain\VariablesGroup\VariablesGroup" fetch="EAGER">
<join-column name="parent" referenced-column-name="id" nullable="true"/>
</many-to-one>
Docs didn't help me much here, since those 2 mappings are described separetely, and might not be designed to be mixed this way.
--
But I'd prefer to avoid querying the Group from DB and populate the Object field when not necessary.
Since I didn't find how Doctrine handles this under the hood, I guess it might not be something supported :(
Technical precisions about this mappings-interraction (or should I say conflict ?) would be welcomed.
Well, some more testing, using inconsistent data entries in my entity seem to clearly demonstrate that such usage isn't intended.
Whild my code wouldn't allow this, if I do save my entity with groupId = 1 and Group = (group Object with ID 2), I get an alternance of values for the saved value in the "parent" column of my database.
I guess this is due to Doctrine skipping the "useless" update, and proceeding to the one which effectively changes the data value.
Since both values are different, the skipped update alternantes.
If my assumptions are right, Doctrine would better pop an error in such a mapping case. (as it does if you map one column to 2 fields).
Maybe I'll file a suggestion/issue, if this seem relevant.

Entity Framework returns different result with AsNoTracking

I use Entity Framework in combination with an Oracle database. If I create a query like
myLinqStatement.ToListAsync()
I get wrong data returned as a result. If I change the statement to
myLinqStatement.AsNoTracking.ToListAsync()
I get the correct data.
I also checked the native SQL query, which is generated by myLinqStatement.ToListAsync(). The generated SQL query is correct, because I get the correct data.
So is there a problem in the mapping? And why is it working with AsNoTracking?
Thanks!
What AsNoTracking does is to retrieve the data without attaching it to the context, hence any changes you apply over the data do not take effect unless you attach it again so that EF knows it should track its changes.
The code snippets you've provided do not show the whole picture, from the moment a context is created, but is it possible that other parts of your code mutate data before you call myLinqStatement.ToListAsync()?
As you mention that myLinqStatement.AsNoTracking.ToListAsync() returns expected data, makes me assume that there are some side effects in your code that AsNoTracking simply is not aware so just returns whatever it finds in your db
I came across this question because I had a similar issue with Entity Framework Core querying a DB view, the issue was cause because view didn't have a key defined, after defining a key for the entity that map to that DB view, the query returned the same result in both cases (using AsNoTracking or without using it).
In T-SQL a key for a DB view can be defined this way:
CREATE UNIQUE CLUSTERED INDEX UQ_MyDBViewName_ColumnKey
ON dbo.MyDBViewName (ColumnKey);
And in code, you can map the key using the [Key] attribute in the corresponding property of the entity or using the EF fluent API. It will depend of what the project is using.
Either way, using AsNoTracking on a query that goes directly to a DB view makes a lot of sense. Also, if for some reason the query of the view does not allow us to define a key for that view, then the option is to use AsNoTracking.
Hope it helps anyone else having the same issue.

Is there a way to sort a content query by the value of a field programmatically?

I'm working on a portal based on Orchard CMS. We're using Orchard to manage the "normal" content of the site, as well as to model what's essentially data for a small application embedded in it.
We figured that doing it that way is "recommended" for working in Orchard, and that it would save us duplicating a bunch of effort in features that Orchard already provides, mainly generating a good enough admin UI. This is also why we're using fields wherever possible.
However, for said application, the client wants to be able to display the data in the regular UI in a garden-variety datagrid that can be filtered, sorted, and paged.
I first tried to implement this by cobbling together a page with a bunch of form elements for the filtering, above a projection with filters bound to query string parameters. However, I ran into the following issues with this approach:
Filters for numeric fields crash when the value is missing - as would be pretty common to indicate that the given field shouldn't be considered when filtering. (This I could achieve by changing the implementation in the Orchard source, which would however make upgrading trickier later. I'd prefer to keep anything I haven't written untouched.)
It seems the sort order can only be defined in the administration UI, it doesn't seem to support tokens to allow for the field to sort by to be changed when querying.
So I decided to dump that approach and switched to trying to do this with just MVC controllers that access data using IContentQuery. However, there I found out that:
I have no clue how, if at all, it's possible to sort the query based on field values.
Or, for that matter, how / if I can filter.
I did take a look at the code of Orchard.Projections, however, how it handles sorting is pretty inscrutable to me, and there doesn't seem to be a straightforward way to change the sort order for just one query either.
So, is there any way to achieve what I need here with the rest of the setup (which isn't little) unchanged, or am I in a trap here, and I'll have to move every single property I wish to use for sorting / filtering into a content part and code the admin UI myself? (Or do something ludicrous, like create one query for every sortable property and direction.)
EDIT: Another thought I had was having my custom content part duplicate the fields that are displayed in the datagrids into Hibernate-backed properties accessible to query code, and whenever the content item is updated, copy values from these fields into the properties before saving. However, again, I'm not sure if this is feasible, and how I would be able to modify a content item just before it's saved on update.
Right so I have actually done a similar thing here to you. I ended up going down both approaches, creating some custom filters for projections so I could manage filters on the frontend. It turned out pretty cool but in the end projections lacked the raw querying power I needed (I needed to filter and sort based on joins to aggregated tables which I think I decided I didn't know how I could do that in projections, or if its nature of query building would allow it). I then decided to move all my data into a record so I could query and filter it. This felt like the right way to go about it, since if I was building a UI to filter records it made sense those records should be defined in code. However, I was sorting on users where each site had different registration data associated to users and (I think the following is a terrible affliction many Orchard devs suffer from) I wanted to build a reusable, modular system so I wouldn't have to change anything, ever!
Didn't really work out quite like I hoped, but to eventually answer the question in your title: yes, you can query fields. Orchard projections builds an index that it uses for querying fields. You can access these in HQL, get the ids of the content items, then call getmany to get them all. I did this several years ago, and I cant remember much but I do remember having a distinctly unenjoyable time with it haha. So after you have an nhibernate session you can write your hql
select distinct civr.Id
from Orchard.ContentManagement.Records.ContentItemVersionRecord civr
join civ.ContentItemRecord cir
join ci.FieldIndexPartRecord fipr
join fipr.StringFieldIndexRecord sfir
This just shows you how to join to the field indexes. There are a few, for each different data type. This is the string one I'm joining here. They are all basically the same, with a PropertyName and value field. Hql allows you to add conditions to your join so we can use that to join with the relevant field index records. If you have a part called Group attached directly to your content type then it would be like this:
join fipr.StringFieldIndexRecord sfir
with sfir.PropertyName = 'MyContentType.Group.'
where sfir.Value = 'HR'
If your field is attached to a part, replace MyContentType with the name of your part. Hql is pretty awesome, can learn more here: https://docs.jboss.org/hibernate/orm/3.3/reference/en/html/queryhql.html But I dunno, it gave me a headache haha. At least HQL has documentation though, unlike Orchard's query layer. Also can always fall back to pure SQL when HQL wont do what you want, there is an option to write SQL queries from the NHibernate session.
Your other option is to index your content types with lucene (easy if you are using fields) then filter and search by that. I quite liked using that, although sometimes indexes are corrupted, or need to be rebuilt etc. So I've found it dangerous to rely on it for something that populates pages regularly.
And pretty much whatever you do, one query to filter and sort, then another query to getmany on the contentmanager to get the content items is what you should accept is the way to go. Good luck!
You can use indexing and the Orchard Search API for this. Sebastien demoed something similar to what you're trying to achieve at Orchard Harvest recently: https://www.youtube.com/watch?v=7v5qSR4g7E0

Linq security - hide columns

I am struggling a bit on what probably is a simple matter or something I misunderstand... But anyway, using Linq entity code first, I am trying to keep some of my tables to be inaccessible from the client, without success.
Using Breeze, I have made a datacontext that holds only the dbsets I want exposed, fine.
But when I write a query using .expand(). For example, let's say I have a posts table which I want to expose, and an Owner table that I want to hide.
Using a query like:
var query = EntityQuery
.from('Posts')
.expand('Owner');
I can still see all the columns from Owner.
So the question is: in Linq, how am I supposed to secure/protect/hide the tables, and/or specific columns, that I want to hide?
After some digging, all I have found is the [JsonIgnore] attribute, which seems insufficient to me.
What is the best way to do this? I feel I am missing something probably huge here, but it's the end of the day around here...
Thanks
If you are using the Breeze's WebApi implementation then Breeze also supports ODataQueryOptions ( see here and here ).
This allows you to mark up your controller methods so as to limit how the query is interpreted. For example, to only allow filtering on your 'Posts' query and therefore exclude the ability to "expand" or "select" 'Owners' from any 'Posts' request you could do the following.
[Queryable(AllowedQueryOptions=AllowedQueryOptions.Filter| AllowedQueryOptions.Top | AllowQueryOptions.Skip)]
public IQueryable<Posts> Posts() {
....
}
Ok apparently my question was already addressed here:
Risks of using OData and IQueryable
I just found it.

How to programmatically hide a linq-to-sql column on a datagridview?

I'm binding a linq Query to a datagridview for the purposes of allowing a user to insert/update/delete. However I'd like to hide the Id column. but I'd rather not hard code the
datagridview.columns[id].visible=false
In the event the id column name changes. I've read that you can walk through reflection to get a run-time list of columns, but I can't have that checked at design-time for a column name. Is it possible?
If I change the query to not include the ID, then I'm hard coding the column list instead of the id column name, so if columns were added, I'd have to go manually update there instead.
If you're using Winforms, then you should have a BindingSource on your Form that is typed to your LINQ-to-SQL type. If you do this, then you'll get full designer support for all of the fields available on that class.
If you're doing a lot of data binding work with LINQ-to-SQL entities (or any other classes, for that matter), I highly recommend checking into HyperDescriptor by Mark Gravell. It speeds up the UI data binding performance considerably by bypassing repeated runtime invocations of the properties on the classes.
Couldn't you do it in the html? Or are you using ASP.NET?
<asp:BoundField DataField="id" Visible="false" />

Resources