SQLite in ASP.NET Core with EntityFrameworkCore

Update: November 4th, 2016. Reformatting – pictures to code examples. Info: Keep in mind that in some code examples, code that was generated by the visual studio template have been omitted. Update: July 11th, 2016. .NET Core and EntityFrameWork Core version 1.0 is upon us! So this guide deserves a little update Step 1: Create … Read more

Cannot access a disposed object. A common cause of this error is disposing a context

This is because of your method return type async void. In general, when you are using async void in your code it’s bad news, because: You can’t wait for its completion Any unhandled exceptions will terminate your process (ouch!) So return async Task instead of async void from your method as follows: public async Task … Read more

What goes into DbContextOptions when invoking a new DbContext?

If you really want to create the context manually, then you can configure it like this: var optionsBuilder = new DbContextOptionsBuilder<ApplicationDbContext>(); optionsBuilder.UseSqlServer(Configuration.GetConnectionStringSecureValue(“DefaultConnection”)); _context = new ApplicationDbContext(optionsBuilder.Options); (The DbContextOptionsBuilder<ApplicationDbContext> class is the type of options argument in services.AddDbContext<ApplicationDbContext>(options =>). But in the controller, you don’t have access to Configuration object, so you would have to expose … Read more

How to get Table Name of mapped entity in Entity Framework Core

Using the Microsoft.EntityFrameworkCore.Relational package in 2.X: var mapping = dbContext.Model.FindEntityType(typeof(YourEntity)).Relational(); var schema = mapping.Schema; var tableName = mapping.TableName; This assumes that dbContext is a instance of class that inherits from DbContext and that you have YourEntity configured there. Note that in EF Core 3.X, [.Relational() provider extensions have been replaced] (https://learn.microsoft.com/en-us/ef/core/what-is-new/ef-core-3.0/breaking-changes#provider) with getters and so … Read more

Working with SQL views in Entity Framework Core

In Entity Framework Core 2.1 we can use Query Types as Yuriy N suggested. A more detailed article on how to use them can be found here The most straight forward approach according to the article’s examples would be: 1.We have for example the following entity Models to manage publications public class Magazine { public … Read more

How to instantiate a DbContext in EF Core

Instantiate new object of DbContext from ConnectionString var connectionstring = “Connection string”; var optionsBuilder = new DbContextOptionsBuilder<ApplicationDbContext>(); optionsBuilder.UseSqlServer(connectionstring); ApplicationDbContext dbContext = new ApplicationDbContext(optionsBuilder.Options); // Or you can also instantiate inside using using(ApplicationDbContext dbContext = new ApplicationDbContext(optionsBuilder.Options)) { //…do stuff }

Could not load file or assembly Microsoft.Extensions.DependencyInjection.Abstractions, Version=1.1.0.0

Since you’re using the project in a .net framework library, there’s an issue with auto-generated binding redirects (might be resolved in the upcoming 15.3 update / 2.0 .net core CLI). To work around it, add this in your cpsroj file (preferably before any <Import> element for a .targets file if present): <PropertyGroup> <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects> <GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType> </PropertyGroup> … Read more

ASP.NET add migration ‘composite primary key error’ how to use fluent API

On EF core .. Composite keys can only be configured using the Fluent API – conventions will never setup a composite key and you can not use Data Annotations to configure one. Here is the Fluent API version : Note: This is just an example. Please adjust it according to your use case. // (In … Read more

AddDbContext or AddDbContextPool

The answer is here (under “DbContext pooling”): https://learn.microsoft.com/en-us/ef/core/what-is-new/ef-core-2.0#dbcontext-pooling DbContext is not thread-safe. So you cannot reuse the same DbContext object for multiple queries at the same time (weird things happen). The usual solution for this has been to just create a new DbContext object each time you need one. That’s what AddDbContext does. However, there … Read more

Auto-increment on partial primary key with Entity Framework Core

Well those Data Annotations should do the trick, maybe is something related with the PostgreSQL Provider. From EF Core documentation: Depending on the database provider being used, values may be generated client side by EF or in the database. If the value is generated by the database, then EF may assign a temporary value when … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)