efcore: Throw a better exception message when attempting to use LocalDB with memory-optimized tables
Initializing a SQL Server Express LocalDB database using Entity Framework Core 3.1 Database.EnsureCreated() throws internal SQL Server exceptions.
Here’s the DbContext class code
using Microsoft.EntityFrameworkCore;
using Repository.Entities;
using Repository.Entities.Lookups;
namespace EF_DB
{
public class Context : DbContext
{
private readonly string _connectionString;
public DbSet<Gender> Genders { get; set; }
public DbSet<User> Users { get; set; }
public Context(string connectionString) : base()
{
_connectionString = connectionString;
Database.EnsureDeleted(); // ------ this throws in case #1 ------
Database.EnsureCreated(); // ------ this throws in case #2 ------
Database.ExecuteSqlRaw("ALTER TABLE Users ADD CONSTRAINT DF_CreatedAt DEFAULT GETDATE() FOR CreatedAt");
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) => optionsBuilder.UseSqlServer(_connectionString);
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Gender>().IsMemoryOptimized().HasData(new[] { new Gender(1, "Herr"), new Gender(2, "Frau"), new Gender(3, "Divers") });
}
}
}
Here’s the test code
using System.Diagnostics;
using System.IO;
using System.Reflection;
using EF_DB;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Repository.Entities;
namespace EF_Tests
{
[TestClass]
public class CreateDbTests
{
private static readonly string _testDbLocation = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "EF-DB.mdf");
[TestMethod]
public void CreateDb()
{
using (Context db = new Context(@"Server=(LocalDB)\MSSQLLocalDB;Integrated Security=true;Database=EF-DB;AttachDbFileName=" + _testDbLocation))
{
foreach (User user in db.Users) Debug.Print(user.FullName);
}
}
}
}
Case 1
When I first called above code by running the test method, I got the following error message after executing Database.EnsureDeleted():
Unable to call into the C compiler. GetLastError = 2.
(Being a Microsoft.Data.SqlClient.SqlError exception.)
Following this analysis I granted myself full access to the C:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\MSSQL\Binn\Xtp directory.
But to no avail. Re-running my test resulted in:
Case 2
After granting myself full access rights to the C:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\MSSQL\Binn\Xtp, the exception message changed to:
Cannot continue the execution because the session is in the kill state.
A severe error occurred on the current command. The results, if any, should be discarded.
(Both being Microsoft.Data.SqlClient.SqlError exceptions, listed in the $exceptions.Errors collection)
The exception was and still is raised when calling Database.EnsureCreated() in above code.
Code to reproduce
For you to be able to reproduce, here’s the source Solution:
About this issue
- Original URL
- State: open
- Created 4 years ago
- Comments: 17 (10 by maintainers)
Note for triage: looks like LocalDb crashes when attempting to create a memory-optimized table. Minimal repro and stack below–removing the call to
IsMemoryOptimizedresolves the issue.Note that LocalDb also leaves the database in an invalid state after crashing.
LocalDB works fine for many scenarios, not many use memory optimized tables…