Base solution for your next web application
Ends in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Execute Raw SQL with EF Core #3559


User avatar
0
bilalhaidar created

Hi, I have a bit complex query that I know how to write in SQL but not in LINQ-To-SQL.

Can I use the DbSset.FromSql() method to execute Raw SQL in a custom repository? (ASP.NET Core / Angular 2).

I dunno if you can help with the linq-to-sql.

I have the following setup:

OU (Region) with Parent Null OU (Camp) with Parent OU Region

This could be repeated many times in a sense I will have many regions and many camps within. (2 level only)

I have an entity that needs to store the OU Id.

Upon creation of this new entity, if the entity belongs to a region only, then OU (Region) Id is stored on the entity. If however, this entity belongs to an OU (Region) and OU (Camp), then the OU (Camp) Id is stored on the entity.

A user would have access (belongs) to OUs. if he belongs to an OU (Region) he/she can see all entities created within the O?OU (Region) and OU(Camp)s within the region. If he/she has access to a OU (Camp) only, he/she can see entities created in that OU (Camp) only.

Here's the SQL Query (entity in this case Blog Post). Can it be done in LINQ? Otherwise, can I use FromSql() method?

Thanks

DECLARE @code NVARCHAR(100) = '00001%'
DECLARE @user INT = 1;

WITH UserOus(OuId) AS
(
	SELECT 
		[ou].[Id]
	FROM
		[AbpUserOrganizationUnits] AS [ous]
	LEFT OUTER JOIN [AbpOrganizationUnits] AS [ou] ON [ou].Id = [ous].OrganizationUnitId
	WHERE [ous].UserId = @user
)
SELECT [bp].[Id], [bp].[Title], [bp].[OrganizationUnitId]
FROM [BlgPosts] AS [bp]
INNER JOIN [AbpOrganizationUnits] AS [ou] ON [bp].[OrganizationUnitId] =[ou].[Id]
INNER JOIN [UserOus] AS [uous] ON [ou].ParentId = [uous].[OuId] OR [ou].[Id] = [uous].OuId
GO 


SELECT TOP (1000) [Id]
      ,[TenantId]
      ,[ParentId]
      ,[Code]
      ,[DisplayName]
      ,[IsDeleted]
      ,[DeleterUserId]
      ,[DeletionTime]
      ,[LastModificationTime]
      ,[LastModifierUserId]
      ,[CreationTime]
      ,[CreatorUserId]
  FROM [MainApp].[dbo].[AbpOrganizationUnits]

SELECT TOP (1000) [Id]
      ,[CategoryId]
      ,[Title]
      ,[Content]
      ,[Tags]
      ,[Status]
      ,[CreationTime]
      ,[CreatorUserId]
      ,[OrganizationUnitId]
  FROM [MainApp].[dbo].[BlgPosts]
  
  SELECT TOP (1000) [Id]
      ,[UserId]
      ,[OrganizationUnitId]
      ,[CreationTime]
      ,[CreatorUserId]
      ,[TenantId]
  FROM [MainApp].[dbo].[AbpUserOrganizationUnits]

5 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @bilalhaidar,

    I don't know if GO statement is supported or not in DbSset.FromSql. It is better to try and find out :).

    But, you can create a stored procedure and run it in DbSset.FromSql.

    Thanks.

  • User Avatar
    0
    bilalhaidar created

    And it will work just fine with ASPNET0 as if I am using Linq To SQL queries (result mapped to objects, etc.) ? how about tracking to edit and save, etc. ? Will this still work?

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Mapping should work because it is done by EntityFramework, not by AspNet Zero. Actually I'm not sure about other operations (update, delete, save) because I haven't tried DbSset.FromSql before.

    But since you are running a custom sql query, ABP will not interfere here.

  • User Avatar
    0
    bilalhaidar created

    Thanks I will try it

  • User Avatar
    0
    nicolaslau created

    @ismcagdas where is the best place to call FromSql in AspNet Zero?