I am using a partitioned view (SQL Server 2012 STD Edition) to split my large Trn
table based on date
I followed the following steps
First created several tables (Trn202425
, Trn202324
, Trn202223
etc) with the following schema
public class Trn202425
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid Id { get; set; }
[ForeignKey("Branch")]
public string BrnCd { get; set; }
public BranchMaster Branch { get; set; }
public DateTime TrnDt { get; set; }
public string TrnNo { get; set; }
}
Configured the model with
builder.ToTable("Trn202425");
builder.HasCheckConstraint("CK_Trn202425_TrnDt", "[TrnDt] >= '2024-04-01' And [TrnDt] <='2025-03-31'");
builder.ToTable("Trn202324");
builder.HasCheckConstraint("CK_Trn202324_TrnDt", "[TrnDt] >= '2023-04-01' And [TrnDt] <='2024-03-31'");
builder.ToTable("Trn202223");
builder.HasCheckConstraint("CK_Trn202223_TrnDt", "[TrnDt] >= '2022-04-01' And [TrnDt] <='2023-03-31'");
Used the Migration to create the above tables in Database
Then created the View
public class TrnMain
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid Id { get; set; }
[ForeignKey("Branch")]
public string BrnCd { get; set; }
public BranchMaster Branch { get; set; }
public DateTime TrnDt { get; set; }
public string TrnNo { get; set; }
}
And configured it with
builder.ToView("TrnMain");
Created a view in the Database [Manually]
CREATE VIEW [dbo].[TrnMain]
WITH SCHEMABINDING
AS
SELECT TrnDt, TrnNo, Id, BrnCd FROM dbo.TrnMain202425
UNION ALL
SELECT TrnDt, TrnNo, Id, BrnCd FROM dbo.TrnMain202324
UNION ALL
SELECT TrnDt, TrnNo, Id, BrnCd FROM dbo.TrnMain202223
Now while inserting the values using EF Core
The entity type 'TrnMain' is not mapped to a table, therefore the entities cannot be persisted to the database. Use 'ToTable' in 'OnModelCreating' to map it.
I changed the line
builder.ToView("TrnMain");
To
builder.ToTable("TrnMain");
And now it is working.
My question is am I doing it correctly or is there any other way to implement the partitioned view
Using .NET 5 & EF Core 5
I am using a partitioned view (SQL Server 2012 STD Edition) to split my large Trn
table based on date
I followed the following steps
First created several tables (Trn202425
, Trn202324
, Trn202223
etc) with the following schema
public class Trn202425
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid Id { get; set; }
[ForeignKey("Branch")]
public string BrnCd { get; set; }
public BranchMaster Branch { get; set; }
public DateTime TrnDt { get; set; }
public string TrnNo { get; set; }
}
Configured the model with
builder.ToTable("Trn202425");
builder.HasCheckConstraint("CK_Trn202425_TrnDt", "[TrnDt] >= '2024-04-01' And [TrnDt] <='2025-03-31'");
builder.ToTable("Trn202324");
builder.HasCheckConstraint("CK_Trn202324_TrnDt", "[TrnDt] >= '2023-04-01' And [TrnDt] <='2024-03-31'");
builder.ToTable("Trn202223");
builder.HasCheckConstraint("CK_Trn202223_TrnDt", "[TrnDt] >= '2022-04-01' And [TrnDt] <='2023-03-31'");
Used the Migration to create the above tables in Database
Then created the View
public class TrnMain
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid Id { get; set; }
[ForeignKey("Branch")]
public string BrnCd { get; set; }
public BranchMaster Branch { get; set; }
public DateTime TrnDt { get; set; }
public string TrnNo { get; set; }
}
And configured it with
builder.ToView("TrnMain");
Created a view in the Database [Manually]
CREATE VIEW [dbo].[TrnMain]
WITH SCHEMABINDING
AS
SELECT TrnDt, TrnNo, Id, BrnCd FROM dbo.TrnMain202425
UNION ALL
SELECT TrnDt, TrnNo, Id, BrnCd FROM dbo.TrnMain202324
UNION ALL
SELECT TrnDt, TrnNo, Id, BrnCd FROM dbo.TrnMain202223
Now while inserting the values using EF Core
The entity type 'TrnMain' is not mapped to a table, therefore the entities cannot be persisted to the database. Use 'ToTable' in 'OnModelCreating' to map it.
I changed the line
builder.ToView("TrnMain");
To
builder.ToTable("TrnMain");
And now it is working.
My question is am I doing it correctly or is there any other way to implement the partitioned view
Using .NET 5 & EF Core 5
Share Improve this question edited Nov 16, 2024 at 8:03 Dale K 27.6k15 gold badges58 silver badges83 bronze badges asked Nov 16, 2024 at 7:30 vcsvcs 3,7354 gold badges19 silver badges15 bronze badges 7 | Show 2 more comments1 Answer
Reset to default 0You can consider use SQL Server Partitioned tables.
Since EF Core doesn't generate the partitioning logic in migrations. You need to manage partitioning yourself via custom SQL scripts during database setup (handled through SQL Server directly). Then, use EF Core to map and query and manipulating partitioned tables.
When query the partitioned tables, you could use EF core to execute SQL Queries or work with Stored Procedure.
ToTable
as you are doing. You can specify a "Table" mapping to a view which essentially tells EF "You can select and insert etc. through this". It is up to the DB Engine whether inserts, updates, & deletes can be performed against the view or not. AFAIK you can insert and update rows automatically across partitions through the view. – Steve Py Commented Nov 17, 2024 at 5:07