Complex update multiple rows with Entity Framework Core - linq

I have to make update on Entity Framework Core on multiple rows.
I have 2 tables: ProjectStep and StepDefinition. ProjectStep has ProjectStepId, ParentStepId(pointing to ProjectStep table) and StepDefinitionId. StepDefinition has StepDefinitionId and ParentStepId(pointing to StepDefinition table). I need to update ParentStepId in the table ProjectStep based on the values in StepDefinition Here is how I would have done it in SQL Server:
UPDATE ps
SET ParentStepId=ps1.ProjectStepId
FROM ProjectStep ps INNER JOIN
StepDefinition sd ON ps.StepDefinitionId = sd.StepDefinitionId INNER JOIN
ProjectStep AS ps1 ON sd.ParentStepId = ps1.StepDefinitionId and ...
and in SQL Server this works like a charm. How do I do this in Entity Framework Core? Wirh Linq preferably.
Thanks

Related

I am studying Oracle, But We are Asked how to Select data from multiple tables without using JOIN clause

For Instance
Suppose You have multiple tables Like these!
**Students(StudId, Sname, SRegNo),
Lecturers(LectId, LectName, Lect Course),
Courses(CourseId, CourseName, Coursemarks)**
How can you Join them by Getting only, Sname, Lectname and CourseMarks

Generated group by sql query does not make sense

I'm using entity framework core for mysql, and i've been running a complex linq query which i'm trying to optimise.
I turned on logging in the mysql server to view the resulting queries from the linq queries.
Oddly, none of it made sense as my complex query that joined 5 tables and performed multiple group bys, where, and order by clause was registered in the logs as 5 separate select all columns from table statements.
So, I tried a simple group by statement for one table. The resulting sql log produced "Select all_columns from table_name order by groupbyid".
Can anyone explain what happened here?
Thanks in advance.
More info as requested:
Sql query:
var queryCommand = (from p in _context.TableExtract group p by p.tableExtractPersonId);
queryCommand.ToList();
Resulting mysql log after:
SELECT .... [very long list of column names]
FROM TableExtract AS p
ORDER BY p.tableExtractPersonId
I've tried two different entity framework libraries: MySql.Data.EntityFrameworkCore(v8.0.17) and Pomelo.EntityFrameworkCore.MySql (v2.2.20) with the same results. I've tried .net core 3.0 and also received the same results. I'm going to try .net standard next.
Ok. I found it:
var queryCommand = (from p in _context.TableExtract group p by p.tableExtractPersonIdinto g select g.Key)
Forces linq to evaluate as a SQL group by. Otherwise apparently it does it's own thing with the group by.

SQL Server - Oracle Linked Server with join

Hare is the scenario:
Main DB Server: SQL Server 2008 R2 with a Linked Server to Oracle 11g.
I have a Stored Procedure that make a query like:
Select t1.a, t1.b, t2.c, t3.d
From LocalTable a inner join LinkedServerName..Schema.Tableb b on a.aNumber= b.id
inner join LinkedServerName..Schema.Tablec c on b.value = c.id
inner join LinkedServerName..Schema.Tabled d on a.someOtherNumber = d.Id
Where a.WhereValue1 = #Parameter1
and b.WhereValue2 = #Parameter2
That turns to be painfully slow. I cannot figure out how use OpenQuery to improve the query since the Where clauses uses parameters (if that is even possible to use).
Is there way to improve the data Retrieval? I'm retrieving millions of records from the Oracle DB.
Thanks you very much.
What I suggest you do is at least create a view on the Oracle side that joins tables b,c,d and join to that view. How many records are in LocalTable? If there are very few (under 10,000 or so) then you are better joining the entire thing on the Oracle side.
What is your overall objective? Are you building a report or trying to identifying differing records so you can merge the data? Are you able to make changes on the oracle side?

how to join two tables using adodb active record in php

please tell me how to use the below query in adodb active records..
SELECT *
FROM students
JOIN class
ON student_class_id = class_id;
Thanks..

Return objects missing records in many to many using EF 5.0

Given the schema below, I'm trying to build an EF query that returns Contacts that are missing required Forms. Each Contact has a ContactType that is related to a collection of FormTypes. Every Contact is required to have at lease one Form (in ContactForm) of the FormTypes related to its ContactType.
The query that EF generates from the linq query below works against Sql Server, but not against Oracle.
var query = ctx.Contacts.Where (c => c.ContactType.FormTypes.Select (ft => ft.FormTypeID)
.Except(c => c.Forms.Select(f => f.FormTypeID)).Any());
I'm in the process of refactoring a data layer so that all of the EF queries that work against Sql Server will also work against Oracle using Devart's dotConnect data provider.
The error that Oracle is throwing is ORA-00904: "Extent1"."ContactID": invalid identifier.
The problem is that Oracle apparently doesn't support referencing a table column from a query in a nested subquery of level 2 and deeper. The line that Oracle throws on is in the Except (or minus) sub query that is referencing "Extent1"."ContactID". "Extent1" is the alias for Contact that is defined at the top level of the query. Here is Devart's explanation of the Oracle limitation.
The way that I've resolved this issue for many queries is by re-writing them to move relationships between tables out of the Where() predicate into the main body of the query using SelectMany() and in some cases Join(). This tends to flatten the query being sent to the database server and minimizes or eliminates the sub queries produced by EF. Here is a similar issue solved using a left outer join.
The column "Extent1"."ContactID" exists and the naming syntax of the query that EF and Devart produce is not the issue.
Any ideas on how to re-write this query will be much appreciated. The objective is a query that returns Contacts missing Forms of a FormType required by the Contact's ContactType that works against Oracle and Sql Server.
The following entity framework query returns all the ContactIDs for Contacts missing FormTypes required by their ContactType when querying against both Sql Server and Oracle.
var contactNeedsFormTypes =
from c in Contacts
from ft in c.ContactType.FormTypes
select new { ft.FormTypeID, c.ContactID};
var contactHasFormTypes =
from c in Contacts
from f in c.Forms
select new { c.ContactID, f.FormTypeID};
var contactsMissingFormTypes =
from n in contactNeedsFormTypes
join h in contactHasFormTypes
on new {n.ContactID, n.FormTypeID} equals new {h.ContactID, h.FormTypeID}
into jointable
where jointable.Count()==0
select n.ContactID;
contactsMissingFormTypes.Distinct();

Resources