I have EF database first project and I would like to build query that will be accessing database using custom aggregate like this below for string concatenation
http://angusmiller.co.za/2010/01/28/clr-user-defined-aggregates-concatenate-strings-with-separator/
Is there any way to do this?
Related
i would like to know if you have any idea how i can achieve this, considering a query stored as string in the configuration file.
I tried to use SqlQuery applied to the DBSet, but the problem is that SqlQuery requires me to select all properties of the required entities in my query. If i don't consider any column, it will complain because is not able to map the query to the entities.
I don't want to select all properties of the entities i want to query.
Thanks
If you are using EF then why not use Database.ExecuteSqlCommand()? It's in the System.Data.Entity namespace.
For example:
int result = db.Database.ExecuteSqlCommand("Non SELECT SQL etc...");
Well, I ended up implementing a mechanism using reflection that basically receives a group of fields to select, and constructs dynamic objects with those fields, so when applied the query with the joins between the entities, will only bring the fields I am looking for.
So, considering Entity1, Entity2, Entity3 with the following relationship
<b>Entity1</b>{
<br/> Entity1Name, <br/> List<*Entity2*> Entity2Items, <br/> etc..
<br/>}
and
<b>Entity2</b> { <br/> Entity2Name, <br/> List<*Entity3*> Entity3Items <br/>}
I can store e.g. the following query in the configuration file, and retrieve the information:
"Entity1.Entity1Name", <br/>
"Entity1.Entity2Items.Entity2Name", <br/>
"Entity1.Entity2Items.Entity3Items.Entity3Name"
Anyway, I was just trying to see if there would be any solution out-of-the-box that would require minimal code changes.
Thank you.
I know that it is possible using #SqlResultSetMapping but I want to select not whole entity from the database but some fields and then map i to my entity using one of the constructor which accept that fields. Is that possible to map result with #EntityResult for only a few #FieldResult? I was trying to do that and all the time I get error which said that there is not specify mapping for some fields which exist in that entity.
The disadvantage of #SqlResultSetMapping is that you have to select all the columns.
The alternate way of doing this manually iterate over the DB result and populate your objects.
Well, if you are using JPA 1.0 your only option (not considering the manual mapping, of course), is to use #SqlResultSetMapping and map the whole table columns. With JPA 2.1 you can add a javax.persistence.ConstructorResult (see docs here) to map only the needed columns.
I have a need to run raw queries on a database. These tables do not have Model classes in my project to map the result onto.
So I want to be able to execute any select query using context.Database
SqlQuery but instead of mapping it a model class, I want to be able to map it to a generic solution like datatable or List<Dictionary<string,string>>.
Is it possible to do such a thing in EntityFramework 6?
I know I can use SqlDataAdapter to execute raw query and map the data to a datatable. I am just looking for a way to do the something with Entity.
It is easy to do a Contains, StartsWith, or EndsWith query in Entity Framework when you know the pattern you'd like to find. But how to do the equivalent of "LIKE [Column]"?
For instance, this is easy:
Name LIKE '%Th'
But how do you do this? where Prefix is a column.
Name LIKE [Prefix]
I tried to use the SqlMethods and got the following error.
LINQ to Entities does not recognize the method 'Boolean Like(System.String, System.String)' method, and this method cannot be translated into a store expression.
I resolved this problem by using LINQ to Entities to bring back the set of data that I wanted, then using LINQ to Objects to filter the data.
I do not think there is currently a way to have a LINQ to Entities query be resolved in a database server as ...
... LIKE [column-name]
First, SqlMethods is for use in LINQ-to-SQL, not LINQ-to-Entities.
The best you can do to use the filter in the database engine (so you don't have to pull all data in memory first) is:
context.Entities.SqlQuery("SELECT * FROM EntityTable WHERE Name LIKE Prefix")
I have some dynamically created tables(using native queries). So I do not have the names in my entity but stored in a db table. Now I want to retrieve datas from each. I want to use only JPA.
To use JPA you must have Entity that maps on to any table that you want to access. If you don't have that then JPA is not for you, and just use JDBC. Can't get simpler.