Ehsan Ghanbari

Experience, DotNet, Solutions

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

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);

        }

 

About Me

Ehsan Ghanbari

Hi! my name is Ehsan. I'm a developer, passionate technologist, and fan of clean code. I'm interested in enterprise and large-scale applications architecture and design patterns and I'm spending a lot of my time on architecture subject. Since 2008, I've been as a developer for companies and organizations and I've been focusing on Microsoft ecosystem all the time. During the&nb Read More

Post Tags
Pending Blog Posts
Strategic design
Factory Pattern
time out pattern in ajax
Selectors in Jquery
Peridic pattern
How to query over Icollection<> of a type with linq
How to use PagedList In asp.net MVC
Domain driven design VS model driven architecture
What's the DDD-lite?
Redis as a cache server
comments powered by Disqus