Can SqlFunctions work with Dynamic Linq? - entity-framework

Is it possible to get SqlFunctions to work with Dynamic Linq? I noticed this post from the SO site which seems to suggest that it is possible, however, every time I run this query below in a .NET project (not .NET Core):
query.Where("SqlFunctions.StringConvert((double?)Property).Contains(\"x\")");
It spits back a message that:
No property or field 'SqlFunctions' exists in type 'Entity'

Worked for me. Check you have created your own custom DLinq with added SqlFunctions type to predefinedTypes as per the post.
Personally I'm adding SqlFunctions dynamically to my DLinq. See https://stackoverflow.com/a/18313782/525788 for guidance.

Related

EF - Eager include grandchildren not working, does not contain definition for 'Select'

I am working in VS 2015 solution with an MVC 5 project and a code library project using EF 6.1. I have redone the entire project from a previous version in VS 2013 hoping it would resolve this problem, but no luck. I am just trying to eager load a child with grandchildren. I tried this:
Test t = db.Tests
.Include("TestsRemoteOBD")
.Include("TestsRemoteOBD.TestsRemoteOBDDtcs")
.FirstOrDefault();
and this:
Test t = db.Tests
.Include(i=>i.TestsRemoteOBD)
.Include(i=>i.TestsRemoteOBD.Select(s=>s.TestsRemoteOBDDtcs))
.FirstOrDefault();
Include using the string works for the child, but not the grandchildren. And with the 2nd query, I'm getting this error:
'TestsRemoteOBD' does not contain a definition for 'Select' and no extension method 'Select' accepting a first argument of type 'TestsRemoteOBD' could be found (are you missing a using directive or an assembly reference?)
I've seen the same question resolved by adding "using System.Data.Entity;", but I had done that a long time ago. No help. What else could I be missing?
Thanks!
Thanks so much, to all those who commented. I would have been banging my head for another day, at least. The way I had written the second statement would have worked fine if the parent [Tests] had a 1-to-many relationship with [TestRemoteOBD], but since the latter was not a collection, there was no 'Select' defined. I have not tested, but I believe it should be something like:
Test t = db.Tests
.Include(i=>i.TestsRemoteOBD)
.Include(i=>i.TestsRemoteOBD.TestsRemoteOBDDtcs)
.FirstOrDefault();
Bump. Found this and thought I'd throw in my own research.
Microsoft's
Documentation
states: To include a reference and then a reference one level down: query.Include(e => e.Level1Reference.Level2Reference)
-- but this doesn't seem to work under even the simplest use case. Executing the query with a .ToList() does not actually include anything. Smells like an obvious bug to me!
I can see 2 workarounds.
First, as Ben here suggests, you can manually iterate over the entities to force EF to load them.
Second, if scoping permits, you can forget the 'using' block and keep a more permanent instance of the dbcontext as a class member so that it's still available, not disposed (System.ObjectDisposedException), when the members are accessed.

Entity Framework Code First use SQL Server datetime2 instead of datetime everywhere

I know this can be accomplished using the Fluent API on a single property, but I want to have this automatically happen for everywhere I use a .NET DateTime in my model. Here's an example of doing a single property:
modelBuilder.Entity<T>()
.Property(f => f.MyDateTimeProperty)
.HasColumnType("datetime2");
But instead of only on this one property I want it to happen everywhere automatically. There is a fix for this in Model First by editing the T4 generation template so I know it can be done there but I need the same thing in Code First.
You want to use a Custom Code First Convention.
There's an example of exactly what you are trying to do here: http://msdn.microsoft.com/en-us/data/jj819164.aspx

Using Concat in TYPO3 Extbase Query

i am struggling a little challenge and i am wondering what the solution might be as I couldn't find an answer in the documentation.
Here is my current working query (part):
$constraints[] = $query->like('kategorie', '%'.$kategorie.'%');
What I want to do is something like:
$constraints[] = $query->like(CONCAT(',', kategorie, ','), '%,'.$kategorie.',%');
Which obviously doesn't work at all. : -)
Every Help is really appriciated. Hope someone know the right syntax. TYPO3 Version is latest 6.1
Thank's alot!
First of all, Extbase query generation didn't change in 6.0, so it's the same for 4.x and 6.x.
Maybe you misunderstand the way Extbase is builing the query.
$query->like('property', '%value%');
will return an object when %value% is contained in "property". Such a property must be defined in the domain model. You can only query properties that have a "getProperty" method in their domain model.
Looking at your code, I think you're finding a way to query for a value being present in a comma-separated value list? Then you would use
$query->in('kategorien', $kategorie);
EDIT:
Your specific problem seems to be that you are not using a true relation for assigning "Kategorien" to your entity. You're using a comma-separated list to hold references to another model. You're discouraged to do so, but it is technically possible and used in the TYPO3 core: The FrontendUser model has a property "usergroup" that works in the same way.
Please have a look at the FrontendUser model: https://git.typo3.org/Packages/TYPO3.CMS.git/blob/HEAD:/typo3/sysext/extbase/Classes/Domain/Model/FrontendUser.php
The annotation of your Kategorien must be correct, e.g.:
/**
* #var \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\My\Extension\Domain\Model\Kategorien>
*/
protected $kategorien;
Also make sure that getKategorien is correct.
If you still have problems, keep in mind that the TCA definition must be working to have Extbase work correctly.
In my opinion, a best practise would be to use the category API introduced in TYPO3 6.0:
http://wiki.typo3.org/TYPO3_6.0#Category

Dynamic resultset with dapper on 3.5

I've got a store procedure call returning a recordset whith field unknown in advance. For some interop reason I need to make it working on 3.5, so I don't have any dynamic support.
Is there some built-in solution in dapper? I did not find any by myself.
If there is no such a solution, does it make sense ( and does it work ) to create on the fly a type exposing the property I would fetch ?
EDIT
I managed to add a completely external solution ( without tweaking the original codebase ) by creating a dynamic object in c# 3.0.
Here is the extension dapper code
and here the factory for the dynamic object.
Well, actually the dynamic support in dapper is implemented via ExpandoObject, which is basically just a dictionary. It would be pretty simple to tweak the existing code to return IDictionary<string,object> instead of dynamic in the case of 3.5

How to get an EntityCollection<class> instead of just a class?

I'm trying to convert a one-to-many relationship in entity framework to a many-to-many.
I've done the admin work and generated a database, which seems to be fine but now I'm running through the resulting errors and trying to get the code to compile. Most of it is fine, but one particular change is giving me a headache - I can't figure out the correct syntax. I'm sure I'm being very stupid here.
The original line was:
addedService.CompiledDatabase = context.Databases.OfType<CompiledDatabase>().First();
But addedService.CompiledDatabase is no longer looking for a single instance of CompiledDatabase, it's looking for a collection: an EntityCollection to be precise.
I can't figure out how to get a linq/lambda query that will return a collection of the required type. Assistance gratefully appreciated.
Something like?
addedService.CompiledDatabase.Attach(context.Databases.OfType<CompiledDatabase>());
...if addedService is already in context.