Blog

Filter posts by Category Or Tag of the Blog section!

Using In Memory Database of Entity Framework Core in asp.net core

Tuesday, 18 September 2018

Using the in-memory database of entity framework is super easy. Create an asp.net core and reference the following NuGet package if it's not referenced( it's been referenced in asp.net core 2 and above):

 

  • Microsoft.EntityFrameworkCore.InMemory

 

Then create the context just like the past:

 

 class SampleDbContext : DbContext

    {

        public SampleDbContext() { }

 

        public SampleDbContext(DbContextOptions<SampleDbContext> options)

        : base(options)

        { }

    }

 

And add the InMemory Configuration in ConfigureService() of startup class:

 
 

 public void ConfigureServices(IServiceCollection services)

        {

            services.Configure<CookiePolicyOptions>(options =>

            {

                // This lambda determines whether user consent for non-essential cookies is needed for a given request.

                options.CheckConsentNeeded = context => true;

                options.MinimumSameSitePolicy = SameSiteMode.None;

            });

 

            services.AddDbContext<SampleDbContext>(context => { context.UseInMemoryDatabase("Sample"); });

 

            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

        }

 

comments powered by Disqus