entity-framework-core

Inject DbContext when database name is only know when the controller action is called

让人想犯罪 __ 提交于 2019-12-08 04:43:19
问题 I have a WebAPI/EF Core back-end application with several possible client databases to connect to (each of the same schema, but only one at a time). Knowledge of which database to connect to will not be known until a WebAPI controller is accessed. By that time, it will be too late to add the DBContext as an injectable object using the call to services.AddDbContext<>() in the ConfigureServices() method of the starup.cs file. Since each instance of DbContext is isolated per request, is there

Entity core Mapping entity types with join table

可紊 提交于 2019-12-08 03:43:30
问题 I've started using entity core and I ran into one problem. I'm using automapper in my project and don't know how to populate my join table(BookAuthor). In MVC I worked with ICollections but entity core does not yet support working with many to many relationship entities and now I have to work with join table. Question: How can i map my Bookviewmodel to the Book? I don't know what should I do with my BookAuthors list. Maybe I shouldn't populate it? I tried something like that but it's not

Union can't be translated to SQL

做~自己de王妃 提交于 2019-12-08 02:34:25
问题 I'm using EFCore 2.2.3 and I have disabled local evaluation. I have the following queries var query1 = companyContext.Companies.Where(c => c.Name == name); var query2 = companyContext.Companies.Where(c => c.Id == 10); If i execute them on their own they work correctly. await query1.ToListAsync(); await query2.ToListAsync(); But if i try to var result = await query1.Union(query2).ToListAsync(); i get the following error: InvalidOperationException: Error generated for warning 'Microsoft

Change the connection string dynamically (per request) on Entity Framework 7 / MVC 6

半世苍凉 提交于 2019-12-08 02:14:41
问题 I have a MVC 6 application on which I need to connect to a different database (i.e. physical file but same schema) depending on who is accessing to it. That is: each customer of the web application will have it's data isolated in an SQL database (on Azure, with different performances, price levels, etc.) but all those databases will share the same relational schema and of course, the Entity Framework context class. var cadConexion = @"Server=(localdb)\mssqllocaldb;Database=DBforCustomer1

Get users last accessed time

只谈情不闲聊 提交于 2019-12-07 23:59:45
问题 I am using IdentityServer 4 (with sql storage) for my asp.net core API. I would like to add a "Last Accessed" field somewhere in the database that can be used to order one of my user types in a user search endpoint. Ideally this would show the last time they performed an action in the system. If I do this in the login method it won't be accurate as it will only update when a token is initially generated. I could add a method to every endpoint that the user accessed but this doesn't feel like

What is the correct syntax to the -Context parameter of EF 7's Add-Migration command?

你。 提交于 2019-12-07 22:25:04
问题 Using the latest pre-release version of EntityFramework 7 (v7.0.0-rc1-final), I have tried to use the Add-Migration PowerShell command through the Package Manager Console in Visual Studio; I have tried to add migrations to my DAL. With a folder structure that looks something like: -Project -DAL -Context.cs I have tried variations of the following command: Add-Migration Initial -OutputDir DAL\Migrations -Context ContextClassName Including, but not limited to: Add-Migration Initial -OutputDir

ASP.NET Boilerplate multiple databases and DbContexts

こ雲淡風輕ζ 提交于 2019-12-07 22:19:54
问题 I want to connect to two databases at once using ASP.NET Boilerplate. I followed this example: https://github.com/aspnetboilerplate/aspnetboilerplate-samples/tree/master/MultipleDbContextEfCoreDemo The problem is that only the first context will have all the Abp tables on it. The second context is an existing database, which has none of the Abp tables on it. When I start up the web app, I get this: System.Data.SqlClient.SqlException: 'Invalid object name 'AbpLanguages'.' So obviously it is

EntityFrameworkCore: How to initialize a Database and seed it the first time user uses an application

走远了吗. 提交于 2019-12-07 17:10:35
问题 I have build a project using Microsoft Visual Studio 2015 and EntityFrameworkCore. I have seed manually a couple of dummy data and I was developing my solution. Now, I want to deploy the in the server, but I get the problem that by starting the application the first time, it crash since it does not find a data base and data. I have googled and I find the solution for Visual Studio 2013 and previous using the CreateDatabaseIfNotExists class that need the package: System.Data.Entity (http://www

Lazy load in Web API Core 2.2

…衆ロ難τιáo~ 提交于 2019-12-07 17:09:12
问题 I am having issues with the lazy loading. I have the following dbcontext. public virtual DbSet<AccountGroupMst> AccountGroupMst {get; set;} I have enabled the lazy loading. services.AddDbContext<DBContext>(x => x.UseSqlServer(Configuration.GetConnectionString("Test")) .UseLazyLoadingProxies()); Model, I have virtual and it is a self referencing table. public class AccountGroupMst { [Key] [Required] public int AccountGroupId { get; set; } [MaxLength(255)] [StringLength(255)] [Required] public

deleting some items from a child collection in ef core

与世无争的帅哥 提交于 2019-12-07 16:13:53
问题 I have a parent table and child table, where the parent has a one-to-many relationship with the children. I want to delete some of the children and I want the parent's child collection to reflect that change. If I delete the selected children using RemoveRange , then the child collection doesn't get updated. If I use Remove to remove the children from the child collection then (apparently) it's not as efficient as using RemoveRange . So I have to use RemoveRange to delete the children