Can't use filter with dynamic linq - entity-framework

I tried to use ef plus for filtering data with following code
_context.Filter<TEntity>(x => x.Where(q => "q.Property == 1"));
And got an error.
Am i doing something wrong?

Disclaimer: I'm the owner of the project Entity Framework Plus
Disclaimer: I'm the owner of the project Eval-Expression.NET
As specified in the introduction: http://entityframework-plus.net/linq-dynamic, LINQ Dynamic require the library Eval-Expression.NET (paid library) to work.
It should now work once you add this library in reference: https://www.nuget.org/packages/Z.Expressions.Eval/

Related

How to use string in select in Entity Framework on .NET 6

I want to make a dynamic query in Entity Framework like this:
var query = db.Select("id, name")
I try to use System.Linq.Dynamic.Core library also test https://github.com/aaubry/DynamicQuery
but it is not working.
I appreciate any help
Solved by using System.Linq.Dynamic.Core library modify select:
var query=db.Student.Select("new(Id,Name)").ToDynamicList()

How do I transfer Mapping code from Entity Framework to Entity Worker Core?

The original code is:
modelBuilder.Entity<OriginalClass>().Map<OtherClass>(x => x.Requires("Field1").HasValue("Value1"));
It doesn't compile and I can't seem to find the equivalent code.
I have found the answer here
https://learn.microsoft.com/en-us/ef/core/modeling/relational/inheritance
My code for core will be something like this
modelBuilder.Entity<OriginalClass>().HasDiscriminator<string>("Field1").HasValue<OtherClass>("Value1").HasValue<OtherClass2>("Value2");
Here is how you could translate it to entityworker
modelBuilder.Entity<OtherClass>().
HasNotNullable(x => x.Field1).
HasDefaultOnEmpty(x=> x.Field1, "the field is empty"));
read Attribute section
https://github.com/AlenToma/EntityWorker.Core/blob/master/Documentation/Attributes.md

Iterating through entity framework models from another related project

I have just started working with .Net and I have created an Entity Framework Model and an associated context for the password reset functionality of a website and I have created this in a class library called MYSITE.Reset.Data with 3 classes (email,mapping,link).
I have now created a windows form application MYSITE.Reset but am having trouble in iterating through my models from the program.cs file. I am not quite sure of the structure of the syntax and I have unsuccessfully tried the following:
foreach(MYSITE.Reset.Data.Maps mp)
Try this:
var maps = from m in context.Maps
select m;
foreach(var map in maps)
{
// do stuff
}

SQL from EntityDataSource

Is there an easy way to see SQL statements generated by EntityDataSource?
SQL Server profiling/tracing is not an option here.
I used to use NHProf a profiler for NHibernate and it is awesome. So I can surely say that you should try the Entity Framework Profiler
I just checked and, as for NHProf, there is a free trial version.
You can cast to ObjectQuery and call ToTraceString:
ObjectSet<User> objectSet = ObjectSet;
var query = (ObjectQuery)(objectSet.Where(u => u.LastName == "Doe").Select(u => u));
string trace = query.ToTraceString();
For tracing/caching you can try the EF Caching and Tracing Provider Wrapper. I haven't had a chance to try it yet, but it's definitely on my to do list.

How to use ADO.net Entity Framework with an existing SqlConnection?

I have an existing asp.net website that uses an SqlConnection.
I have added the ADO.net Entity Framework.
I have successfully connected to the database and created the .edmx file.
I am able to connect through the Entity Framework with the connectionstring that is automatically generated.
I want to use the existing SqlConnection object that I use throughout the site for the Entity Framework connection.
I do not want to have to use a second database connection for the one page that is going to use the ADO.net Entity Framework and I don’t want to change the entire site to use the new Entity Framework connection string.
Thanks for any help you can provide.
That forum post has the answer:
MetadataWorkspace workspace = new MetadataWorkspace(
new string[] { "res://*/" },
new Assembly[] { Assembly.GetExecutingAssembly() });
using (SqlConnection sqlConnection = new SqlConnection(connectionString))
using (EntityConnection entityConnection = new EntityConnection(workspace, sqlConnection))
using (NorthwindEntities context = new NorthwindEntities(entityConnection))
{
foreach (var product in context.Products)
{
Console.WriteLine(product.ProductName);
}
}
"res://*/" is the part of your EF connection string that describes the location of your xml mapping files - in this case embedded resources in the current assembly.
You can do this by using the constructor of your generated ObjectContext that accepts an EntityConnection. When you create the EntityConnection you pass in your SqlConnection.
Andrew Peters,
Thank you for your answer.
I have been going around and around with the System.Data.EntityClient.EntityConnection.
It’s right there at my finger tips but I cannot seem to get the MetadataWorkspace parameter to work.
This is the closest example I have found (the post marked Answer):
http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/dd7b1c41-e428-4e29-ab83-448d3f529ba4/
Thanks for any help.