Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Newbie question: How on earth can I use this?? #95

Closed
hwmaat opened this issue Jan 17, 2021 · 10 comments
Closed

Newbie question: How on earth can I use this?? #95

hwmaat opened this issue Jan 17, 2021 · 10 comments
Labels
question Further information is requested

Comments

@hwmaat
Copy link

hwmaat commented Jan 17, 2021

I have a .netcore 5.0 web api project. Mysql database on the backend with Entity framework core.
Ran into your Linq2db. And I would love to be able to use the extensions like CTE, Left join etc....
The description tells me: 'is an integration of LINQ To DB with existing EntityFrameworkCore projects'

And I have an exsiting EntityFrameworkCore project.

And furthermore:
'In your code you need to initialize integration using following call:
LinqToDBForEFTools.Initialize();
After that you can just call DbContext and IQueryable extension methods, provided by LINQ To DB.'

The questions:
Where would I put the 'LinqToDBForEFTools.Initialize();' ? In the startup.cs in configure or configure services?
And how would I use it in my existing services? If I try to use e.g. LeftJoin, I can add using Linq2db etc. But the it just invalidates all existing code in that service.

It would be nice if you could give an example project with a simple backend like SQLite!

Thanks

@sdanyliv
Copy link
Member

You don't have to use Initialize in 99.9% it will be initialised automatically. You can do that once, for example on startup.
You have any Linq query written using EF:

var query = ctx.SomeTable.Where(...);

After (this is the main part)

query = query.ToLinqToDB();

Query is fully switched to alternative parser. After this call control over query execution is taken by linq2db

@sdanyliv sdanyliv added the question Further information is requested label Jan 17, 2021
@hwmaat
Copy link
Author

hwmaat commented Jan 17, 2021

Ok!

Got it and works. Nice job. I like it especially because I am using an existing database. I'm 'abusing' ef core to create a middle layer with mainly Poco's being used in services. I have to move 900 stored procedures away from SQL server to a middle layer and make it independent from SQL server.
If I can use your tool that could take away a bit of the burden.

Need to check however how to deal with the missing change-tracking. Did not pay attention to that subject yet, just took it out of the box.
Any tips on that subject?

Noticed also that it is not good to mix using Microsoft.EntityFrameworkCore; with using LinqToDB.EntityFrameworkCore in one class. All .ToListAsync() and .AsnoTracking() used in ef core LinqSql error off.

Apparently have to choose one or the other.

Cheers! Herman

@hwmaat
Copy link
Author

hwmaat commented Jan 17, 2021

Having said all of this, it looks like I cannot turn off tracking now! When using only LinqToDB to extract a list of clients, it tracks every entity!
The thing is here, I do not want to track that type of date since that is just read only.

I have created a little test project here: https://github.com/hwmaat/Linq2DbEftest
If I look at the immediate window where I write the logs, IT show the tracking being done!

Any clue how to stop the change tracking?

@sdanyliv
Copy link
Member

Have you tried AsNoTracking()?
Also it can disabled by default LinqToDBForEFTools.EnableChangeTracker = false

@hwmaat
Copy link
Author

hwmaat commented Jan 18, 2021

LinqToDBForEFTools.EnableChangeTracker = false works, that would be a solution. It turns it off for all .
But it would be nice to be able to use some change tracking when required....

AsNoTracking() does not exist on Ling2Db.EntityFrameworkCore. (from what I checked)
So there is no way to turn it off or on for a single query.

I could not find a way to turn off change tracking just for the following query:

			var items = from cl in _dbContext.Clients
						select cl;
			items = items.ToLinqToDB();
			var result = await items.ToListAsync().ConfigureAwait(true);
			return result;

@sdanyliv
Copy link
Member

AsNoTracking() is a part of EF it must exists.

@hwmaat
Copy link
Author

hwmaat commented Jan 18, 2021

That is correct. BUT IF I add this to the code example above like:
var items = from cl in _dbContext.Clients
select cl;
items = items.ToLinqToDB();
var result = await items.AsNoTracking().ToListAsync().ConfigureAwait(true);
return result;
If have to add 'using Microsoft.EntityFrameworkCore;' And that as a consequence will make that I cannot use .ToListAsync() anymore, that will redline as an error.

Don't know what the solution would be?

@sdanyliv
Copy link
Member

Use .ToListAsyncLinqToDB(). Please reread small readme of this project.

@hwmaat
Copy link
Author

hwmaat commented Jan 18, 2021

OK! thanks a lot! also for the amazingly quick response.

@sdanyliv
Copy link
Member

URW, closing then. I hope our extension will save your time as it does for others,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Development

No branches or pull requests

2 participants