My application uses two lookup tables I want to be Memory Optimized Tables.
There seems to be no Attribute yet to declare this;
Is there a possibility to hook into the table creation process and look for an custom attribute and modify the create table command?
I use Entity Framework 6.1 with Code First.
I seek a way to make it declarative, so if e.g. EF 6.2 or newer version supports it official, i can drop my hack.
What are Memory Optimized Tables? A Memory Optimized Table, starting in SQL Server 2014, is simply a table that has two copies, one in active memory and one durable on disk whether that includes data or just Schema Only, which I will explain later.
Starting in SQL Server 2016, the query plan for a memory-optimized table can scan the table in parallel. This improves the performance of analytical queries. Hash indexes also became scannable in parallel in SQL Server 2016. Nonclustered indexes also became scannable in parallel in SQL Server 2016.
Create a memory-optimized data filegroup and add a container to the filegroup. Create memory-optimized tables and indexes. For more information, see CREATE TABLE (Transact-SQL). Load data into the memory-optimized table and update statistics after loading the data and before creating the compiled stored procedures.
I'd replace the CreateTable with the specific sql you want to optimise your database, called in the Up migration using Sql("create table etc..").
This is restricted to Sql Server 2014 and further restricted by other requirements for memory optimisation. Not worth the candle trying to do it with attributes within EF.
Maybe you can create an Entity Framework DB initializer that drop and recreate the table?
Something like (pseudo code!):
public class MyDbInitializer : CreateDatabaseIfNotExists<MyDbContext>
{
protected override void Seed(MyDbContext context)
{
context.Database.ExecuteSqlCommand("DROP TABLE MyTable");
context.Database.ExecuteSqlCommand("CREATE TABLE MyTable (bla bla bla) WITH (MEMORY_OPTIMIZED=ON)");
}
}
I can't test this feature because I don't have SQL 2014 but I have used the Seed
method to alter my database in many situations so it should work.
You can then assign your database initializer using:
System.Data.Entity.Database.SetInitializer(new MyDbInitializer());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With