How can i use Entity Framework Code First Approach to perform querying and grouping of my data results from SQL Server to get results shown on this image:
I have tried to write some code as shown below but Entity Framework doesn't like and it throws some exceptions :
complaining that data reader can't find some columns to match columns i have specified in the following Entity class which i have created in this Class.
I was thinking may be the T-SQL Query below are not supported in Entity Framework!
SELECT CASE WHEN GROUPING && GROUP BY
.....................................................................................................
Below is Class ContractBOQ
and Context class MyFirstDBContext
Table("ContractBOQ")] // map to an existing Table called ContractBOQ
public class ContractBOQ
{
[Key]
[Column("SN")]
public int SN { get; set; }
[Required]
[Column("Item Code")]
public string Item_Code { get; set; }
[Required]
[Column("Description")]
public String Description { get; set; }
[Required]
[Column("Unit")]
public string Unit { get; set; }
[Required]
[Column("Quantity")]
public decimal Quantity { get; set; }
[Required]
[Column("Approaved Rate")]
public decimal Approaved_Rate { get; set; }
[Required]
[Column("Contract Rate")]
public decimal Contract_Rate { get; set; }
[Required]
[Column("Approved Amount")]
public decimal Approved_Amount { get; set; }
[Required]
[Column("Contract Amount")]
public decimal Contract_Amount { get; set; }
[Required]
[Column("BOQ Part")]
public string BOQ_Part { get; set; }
[Required]
[Column("Contract ID")]
public string Contract_ID { get; set; }
[Required]
[Column("Road Name")]
public string Road_Name { get; set; }
[Required]
[Column("Road Code")]
public string Road_Code { get; set; }
}
public class MyFirstDBContext : DbContext
{
public virtual DbSet<ContractBOQ> ContractBOQ { get; set; }
}
I converted the T-SQL below into stored Procedures and i named it GetAllContracts
so that i can execute it as Query by passing it to the Context.Database.Sqlquery ("GetAllContracts")
method of Context
class.
Below is a stored procedure GetAllContracts i created:
CREATE OR ALTER PROC [dbo].[GetAllContracts] AS BEGIN
Select case when grouping([SN])=1 THEN 'Sub-Total:'
else CAST( SN as nvarchar) end SN,
case when grouping ([Item Code])=1 THEN [Road Name]
else [Item Code] end as [Item Code],
case when grouping ([Description])=1 THEN [BOQ Part]
else [Description] end as [Description],
case when grouping ([Unit])=1 THEN '* * *' ELSE [Unit]
end as [Unit],
[Quantity],[Approaved Rate] ,[Contract Rate],
sum([Approved Amount]) as[Approved Amount]
From [MyFirstDB].[dbo].[ContractBOQ]
where [Contract ID]='RH/NTC/2019-2020/Q/01'
group by grouping sets
((SN,[Item Code],[Description],[Unit] ,[Quantity] ,
[Approaved Rate] ,[Contract Rate],[Approved Amount],
[Contract Amount],
[BOQ Part],[Contract ID],[Road Name],[Road Code]),
([Road Name],[BOQ Part]),([Road Name]),()) ORDER BY
GROUPING([Road Name])
END
I then Created a Sub named LoadGrid ( ) which I call it during Form1_Load ( ) event of the form 'Form1'
private void LoadGrid()
{
Application.DoEvents();
Cursor.Current = Cursors.WaitCursor;
using (var db = new MyFirstDBContext())
{
var data = db.ContractBOQ.SqlQuery("GetAllContracts");
dgvData.DataSource = data.ToList();
dgvData.Refresh();
dgvData.ReadOnly = true;
}
Cursor.Current = Cursors.Default;
}
These are columns to the existing Table Named ContractBOQ in Database:
When i run i get this error:
Any help to fix this error and make it work..