entity-framework-core

How is the difference between add or delete an entity from the dbSet and from local property?

痴心易碎 提交于 2019-12-06 16:46:10
When I want to delete a property, I have two options: myDbContext.MyType.Add(myEntity); myDbContext.MyType.Local.Add(myEntity); In both cases, at least in my case, don't see differences, but I don't know really if it has a different behavior or not. Is it really the same or not? Thanks. Based on documentation of methods: myDbContext.MyType.Add(myEntity); /// Begins tracking the given entity, and any other reachable entities that are /// not already being tracked, in the <see cref="EntityState.Added" /> state such that /// they will be inserted into the database when <see cref="SaveChanges()" /

How to set command timeout in aspnetcore/entityframeworkcore

£可爱£侵袭症+ 提交于 2019-12-06 16:35:54
问题 The place where the command timeout is set is no longer the same as earlier versions. However, I cannot find anywhere that says how to change this. What I am doing is uploading very large files which takes longer than the default 30 seconds to save. Note that I ask about Command Timeout, not Migration Timeout as in another question. 回答1: If you're using the DI container to manage the DbContext (i.e. you're adding the DbContext to the service collection), the command timeout can be specified

Modelling folder structure in Entity Framework Core

旧时模样 提交于 2019-12-06 16:10:58
问题 I would like to save an hierarchical folder structure into a SQL database. The class would like this: public class Folder { public Folder() { Children = new List<Folder>(); } public string Name { get; set; } public int Id { get; set; } public int? ParentId { get; set; } public Folder Parent { get; set; } public ICollection<Folder> Children { get; set; } } I'm trying to map it using Entity Framework Core: builder.Entity<Folder>() .HasKey(i => i.Id); // Relation 1 builder.Entity<Folder>()

Execute non-query procedure not working asp.net core

被刻印的时光 ゝ 提交于 2019-12-06 15:53:30
问题 I want to execute a stored procedure which returns three values (Email, Name, CompanyID) and get one parameter (CompanyID) but it's not working. I have created a class with these properties and a stored procedure which returns the data. By it is showing DatabaseFacade error. My code is: List<MyClass> AppUser = new List<MyClass>(); //Class with three properties SqlParameter param1 = new SqlParameter("@CompanyID", CompanyID); AppUser = _context.Database.SqlQuery<CC>("GetUserAndRolesForCompany",

ASP.NET Core only returning first value of a collection in an entity

百般思念 提交于 2019-12-06 14:59:54
问题 I have a class MyEntity with a collection within it: public class MyEntity { [Key] public int MyId { get; set; } [Display(Name = "Process Name")] public string ProcessName { get; set; } public ICollection<Step> ProcessSteps { get; set; } } A one-many relationship with class Step: public class Step { ... [ForeignKey("MyEntityForeignKey")] public MyEntity { get; set; } } Below is the API call to return a specified MyEntity: public async Task<IActionResult> MyEntity(int? id) { if (id == null) {

UserManager throws exception - Unable to track an entity because primary key property 'Id' is null - after upgrading from .Net Core 2.2 to 3.0

旧巷老猫 提交于 2019-12-06 14:33:47
I've used the custom implementation of User which is derivated from IdentityUser using Asp.Net Core Identity : public class AppUser : IdentityUser { ... } If I call the method: var identityResult = await UserManager.CreateAsync(user); I get the error: System.InvalidOperationException: Unable to track an entity of type 'AppUser' because primary key property 'Id' is null. It works perfectly with version of Microsoft.AspNetCore.Identity.EntityFrameworkCore - 2.2.0 , but after upgrading to 3.0.0 - it doesn't work. I get same error during testing of the creation of user as well, there I use

Get users last accessed time

孤街醉人 提交于 2019-12-06 14:06:28
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 the right solution as it would be repeating myself. What is the correct place and method for

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

一曲冷凌霜 提交于 2019-12-06 09:18:15
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 DAL\Migrations -Context Project.Namespace.DAL.ContextClassName Add-Migration Initial -OutputDir DAL

How to add DbContext based on environment in ASP.net Core

随声附和 提交于 2019-12-06 09:07:54
问题 This is how I am currently adding my DbContext in my ConfigureServices method in Startup.cs: public void ConfigureServices(IServiceCollection services) { ..... services.AddDbContext<MyDbContext>(options => options.UseMySQL(Configuration.GetConnectionString("DefaultConnection"))); ..... } And my connection string is stored in my appsettings.json file, like this for example: { .... "ConnectionStrings": { "DefaultConnection": "server=localhost;user id=root;password=root;database=mydb;sslmode

How do I run ASP.NET Core Entity Framework migrations from Visual Studio Team Services

只愿长相守 提交于 2019-12-06 08:11:26
问题 I've got a Web API project created using ASP.NET Core 1.1. I use Entity Framework Core Migrations. Locally, that all works well. However, I'm trying to use Visual Studio team services to automatically run the migrations when I do a release and can't figure out how to do this. Is there some inbuilt component, or should I try to get the dotnet ef tools installed on the agent and run it that way? 回答1: I would suggest using the dotnet ef tools (during VSTS Build) to generate a .sql script which