entity-framework-core

How to get user information in DbContext using Net Core

99封情书 提交于 2019-11-30 03:28:27
I am trying to develop a class library in which i want to implement custom DbContext . In the SaveChanges method of the DbContext , i need to get current user’s information(department, username etc.) for auditing purpose. Some part of the DbContext code is below: public override int SaveChanges() { // find all changed entities which is ICreateAuditedEntity var addedAuditedEntities = ChangeTracker.Entries<ICreateAuditedEntity>() .Where(p => p.State == EntityState.Added) .Select(p => p.Entity); var now = DateTime.Now; foreach (var added in addedAuditedEntities) { added.CreatedAt = now; added

No mapping to a relational type can be found for the CLR type 'Int32[]'

半腔热情 提交于 2019-11-30 03:02:52
问题 When I execute a SQL Server stored procedure from Entity Framework Core (v2.0) in my ASP.NET Core project, I get this exception: InvalidOperationException: no mapping to a relational type can be found for the CLR type 'Int32[]' The SQL Server stored procedure code looks like this: CREATE PROCEDURE [dbo].[sp-UpdateProductOrderAndStock] @customerOrderID INT, @qty INT AS DECLARE @customerProductID INT SET @customerProductID = (SELECT CustomerProductID FROM dbo.CustomerOrder WHERE ID =

Adding DbContextOptions in Startup.cs not registering data store

我的未来我决定 提交于 2019-11-30 02:57:25
问题 My problem is that the below code does not register a data store during startup. This is the specific "error" statement I get in the response from the application: An unhandled exception occurred while processing the request. InvalidOperationException: No data stores are configured. Configure a data store by overriding OnConfiguring in your DbContext class or in the AddDbContext method when setting up services. Microsoft.Data.Entity.Storage.DataStoreSelector.SelectDataStore

Preventing tracking issues when using EF Core SqlLite in Unit Tests

半世苍凉 提交于 2019-11-30 02:53:55
问题 I'm writing a unit test to test a controller action that updates an EF core entity. I am using SQLLite, rather than mocking. I set up my database like this: internal static ApplicationDbContext GetInMemoryApplicationIdentityContext() { var connection = new SqliteConnection("DataSource=:memory:"); connection.Open(); var options = new DbContextOptionsBuilder<ApplicationDbContext>() .UseSqlite(connection) .Options; var context = new ApplicationDbContext(options); context.Database.EnsureCreated()

ASP.NET 5 EntityFramework.Core 7.0.0-rc1-final issue - Compiler wants 7.0.0.0 to be referenced which is not found

[亡魂溺海] 提交于 2019-11-30 02:42:07
问题 I am having the same issue. I have added the following dependencies in my project.json file: "dependencies": { "EntityFramework": "7.0.0-beta4", "EntityFramework.Core": "7.0.0-rc1-final", "EntityFramework.SqlServer": "7.0.0-beta8", "EntityFramework.Commands": "7.0.0-rc1-final" }, "commands": { "web": "Microsoft.AspNet.Server.Kestrel", "ef": "EntityFramework.Commands" }, ... I used dnu install EntityFramework and dnu install EntityFramework.SqlServer to install the packages. "EntityFramework":

No database providers are configured EF7

爷,独闯天下 提交于 2019-11-30 02:14:15
问题 I seem to be getting this error message when using Entity Framework 7 and MVC6 System.InvalidOperationException No database providers are configured. Configure a database provider by overriding OnConfiguring in your DbContext class or in the AddDbContext method when setting up services. I believe i have done everything i am supposed to be doing, so maybe its a bug. I am using version 7.0.0-beta7 of Entity Framework. I have setup my DbContext, an interface so i can mock DbContext (was needed

No service for type Identity.UserManager when using multiple identity users

旧街凉风 提交于 2019-11-30 01:54:40
问题 My setup Currently, I have two models that inherit from ApplicationUser , which inherits IdentityUser . The user classes are: public abstract class ApplicationUser : IdentityUser { [PersonalData] public string FirstName { get; set; } [PersonalData] public string LastName { get; set; } [NotMapped] public string FullName => $"{FirstName} {LastName}"; } public class StudentUser : ApplicationUser { [PersonalData] [Required] public string StudentNumber { get; set; } // A user belongs to one group

Add constraint to existing SQLite table

泪湿孤枕 提交于 2019-11-30 01:42:27
问题 I'm using SQLite, which doesn't support adding a constraint to an existing table. So I can't do something like this (just as an example): ALTER TABLE [Customer] ADD CONSTRAINT specify_either_phone_or_email CHECK (([Phone] IS NOT NULL) OR ([Email] IS NOT NULL)); Are there any workarounds for this scenario? I know: I can add a constraint for a new table, but it isn't new (and it's generated by my ORM, EF Core) I can do a "table rebuild" (rename table, create new one, copy old data, drop temp

Entity Framework Core - IN clause equivalent

橙三吉。 提交于 2019-11-30 01:29:58
问题 I wonder how to convert this sql query to an entity framework query. SELECT * FROM Post WHERE PostId IN ( SELECT PostId FROM BlogPost WHERE BlogId = &blogId); I am trying to get a list of Posts with a given category id. Database simplified: Blog (the category for the post): BlogId Title .. Post: PostId Title .. BlogPost for combining the two tables and letting you have multiple categories per post: PostId BlogId This is what I already have, but ofcourse the query is not working: public async

DbContext for background tasks via Dependency Injection

给你一囗甜甜゛ 提交于 2019-11-30 00:20:57
I am probably not thinking in the right direction. I am fairly new to Dependency Injection and ASP.Net Core. I have a ASP.Net core website, and one of the tasks is to import data from an excel sheet to a database that a user will upload. The excel sheets can be huge and the data transformation tasks are time taking, hence I wish to perform them in the background. i.e. The user will upload the sheet, the response will be sent immediately and the background job/thread will import the data. I am trying to run the background job by: Task.Run(() => ProcessImport(model)); The problem I run into is