dbcontext

How to change registered (Simple Injector) DbContext's connection string after user's authorization?

余生长醉 提交于 2020-08-09 08:14:43
问题 I'm building ASP.NET Core Web Api with Simple Injector and have the following problem: The data for each user is stored in his individual base (but with the same schema), which is known only after he authorizes himself. So... I can give DbContext it's proper connection string only after user's authorization. What is the best way to accomplish this? For now I am storing the connection string in HttpContext custom claim and am refering to it with a use of a static helper class in DbContext 's

How to change registered (Simple Injector) DbContext's connection string after user's authorization?

巧了我就是萌 提交于 2020-08-09 08:13:54
问题 I'm building ASP.NET Core Web Api with Simple Injector and have the following problem: The data for each user is stored in his individual base (but with the same schema), which is known only after he authorizes himself. So... I can give DbContext it's proper connection string only after user's authorization. What is the best way to accomplish this? For now I am storing the connection string in HttpContext custom claim and am refering to it with a use of a static helper class in DbContext 's

.Net EF Core 2.1 Get DbContext from IQueryable argument

爱⌒轻易说出口 提交于 2020-08-07 15:12:23
问题 I have an IQueryable extension method: public static void SomeExt<T>(this IQueryable<T> query, DbContext context) {...} and I would like to know if there is some way to get DbContext from query so that DbContext argument could be removed leaving only: public static void SomeExt<T>(this IQueryable<T> query) {...} I have tried something like this Access DataContext behind IQueryable but its not working, getting zero fields. Also there is way to get it from DbSet Can you get the DbContext from a

Same DbContext, Several databases using EF Core

一曲冷凌霜 提交于 2020-08-06 05:54:57
问题 In my App, I have some data which are stored on different databases (e.g. identity and User Info are stored separately to Content-related stuff for historical reasons). However I would like to be able to get advantage of the '.Include' method of EF Core, but as the data is stored in a different DB, is there a way to query two databases in the same DbContext? 回答1: One option I am seeing is to use views to get all the information from the 2nd database back to the local database. If using SQL

Get List of Modified Objects within Entity Framework 7

こ雲淡風輕ζ 提交于 2020-06-11 20:10:21
问题 I am stumped - upgrading to Entity Framework 7 and I typically override the SaveChanges inside the DbContext to be able to get a list of all the Modified Objects before it changes. Ultimately I have a script that fires that tracks the previous version in a database. In Entity Framework 6 I would get the model changes like so: var oc = ((IObjectContextAdapter)this).ObjectContext; var modifiedItems = oc.ObjectStateManager.GetObjectStateEntries(EntityState.Modified | EntityState.Deleted); List

Ignore duplicate entries and commit successful ones on DbContext.SaveChanges() in EF Core

放肆的年华 提交于 2020-05-27 04:25:07
问题 I have an ASP .Net Core 2.2 Web API. In one of my controller actions, I am adding a bunch of rows to a MySQL database table (I'm using Pomelo). So for example: _dbContext.AddRange(entities); _dbContext.SaveChanges(); The entities I'm adding have two primary keys (composite primary key) and the keys are already populated in the entities collection when I add them to DbContext (i.e. I am setting the keys myself - there is no "auto increment" or anything like that where the database generates

Ignore duplicate entries and commit successful ones on DbContext.SaveChanges() in EF Core

扶醉桌前 提交于 2020-05-27 04:24:25
问题 I have an ASP .Net Core 2.2 Web API. In one of my controller actions, I am adding a bunch of rows to a MySQL database table (I'm using Pomelo). So for example: _dbContext.AddRange(entities); _dbContext.SaveChanges(); The entities I'm adding have two primary keys (composite primary key) and the keys are already populated in the entities collection when I add them to DbContext (i.e. I am setting the keys myself - there is no "auto increment" or anything like that where the database generates

Ignore duplicate entries and commit successful ones on DbContext.SaveChanges() in EF Core

丶灬走出姿态 提交于 2020-05-27 04:23:17
问题 I have an ASP .Net Core 2.2 Web API. In one of my controller actions, I am adding a bunch of rows to a MySQL database table (I'm using Pomelo). So for example: _dbContext.AddRange(entities); _dbContext.SaveChanges(); The entities I'm adding have two primary keys (composite primary key) and the keys are already populated in the entities collection when I add them to DbContext (i.e. I am setting the keys myself - there is no "auto increment" or anything like that where the database generates

Autofac ResolvedParameter/ComponentContext not working

你离开我真会死。 提交于 2020-05-22 10:05:07
问题 I am using asp.net core along with Entity Framework Core . My scenario here is, I want to change the connection string at runtime based on HttpContext query string value. I am trying to pass ResolvedParameter with Reflection components as documented. But, It is not getting registered when I resolve this. Here below, I have attached my code snippet. Autofac Registration class: public class DependencyRegistrar : IDependencyRegistrar { public virtual void Register(ContainerBuilder builder) {

How do I mock DbContext using NSubstitute and then add/remove data

↘锁芯ラ 提交于 2020-05-12 15:42:20
问题 I need to mock EF's DbContext . I use the approach here and it works well. // mock a DbSet var mockDbSet = Substitute.For<DbSet<Foo>, IQueryable<Foo>>(); var data = new List<Foo>().AsQueryable(); ((IQueryable<Foo>)mockDbSet).Provider.Returns(data.Provider); ((IQueryable<Foo>)mockDbSet).Expression.Returns(data.Expression); ((IQueryable<Foo>)mockDbSet).ElementType.Returns(data.ElementType); ((IQueryable<Foo>)mockDbSet).GetEnumerator().Returns(data.GetEnumerator()); // now add it to a mock