entity-framework-core

Delete entities without loading them into memory

不问归期 提交于 2020-01-03 07:12:12
问题 In Entity Framework Core I have the following Entity: public class File { public Int32 Id { get; set; } public Byte[] Content { get; set; } public String Name { get; set; } } And I have a list of files ids which I need to delete: List<Int32> ids = new List<Int32> { 4, 6, 8 }; // Ids example How can I delete the 3 files without loading each file Content property? _context.Files.Remove(??); I do not want to load each file Content property as it is big in size. 回答1: If you are sure the all Ids

The instance of entity type 'TestType' cannot be tracked because another instance of this type with the same key is already being tracked

有些话、适合烂在心里 提交于 2020-01-03 05:14:08
问题 I am getting the below exception when I try to do: context.Entry(testType).State = EntityState.Modified; System.InvalidOperationException: The instance of entity type 'TestType' cannot be tracked because another instance of this type with the same key is already being tracked. When adding new entities, for most key types a unique temporary key value will be created if no key is set (i.e. if the key property is assigned the default value for its type). If you are explicitly setting key values

The instance of entity type 'TestType' cannot be tracked because another instance of this type with the same key is already being tracked

試著忘記壹切 提交于 2020-01-03 05:14:03
问题 I am getting the below exception when I try to do: context.Entry(testType).State = EntityState.Modified; System.InvalidOperationException: The instance of entity type 'TestType' cannot be tracked because another instance of this type with the same key is already being tracked. When adding new entities, for most key types a unique temporary key value will be created if no key is set (i.e. if the key property is assigned the default value for its type). If you are explicitly setting key values

Auto migration in EF Core in three tier application

一个人想着一个人 提交于 2020-01-02 06:21:25
问题 i have three tier application in Asp.net Mvc Core and use EF core, now i want create auto migration , i have DAL layer that my context available here public class AdminContext : DbContext { public AdminContext(DbContextOptions<AdminContext> options) : base(options) { } protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); modelBuilder.Entity<AdsAdsCategory>() .HasKey(bc => new { bc.AdsId, bc.AdsCategoryId }); modelBuilder.Entity

Connect to Azure SQL server via AAD Authentication using EF Core

我怕爱的太早我们不能终老 提交于 2020-01-02 05:13:51
问题 Azure SQL servers support AAD authentication, and I want my applications to connect to SQL server using AAD authentication and not SQL server admin credentials. I am using EF core to connect to SQL, how do I enable EF Core to use AAD authentication. My application is deployed in Azure App Service and it has MSI extension enabled, so I am looking forward to using Microsoft.Azure.Services.AppAuthentication for getting the token from AAD. 回答1: Connect to Azure SQL server via AAD Authentication

Avoid 'Discriminator' with AspNetUsers, AspNetRoles, & AspNetUserRoles

岁酱吖の 提交于 2020-01-01 16:09:31
问题 I am extending IdentityUser , IdentityUserRole , and IdentityRole like this: public class ApplicationUser : IdentityUser { public string FullName { get; set; } public virtual ICollection<ApplicationIdentityUserRole> Roles { get; } = new List<ApplicationIdentityUserRole>(); } public class ApplicationIdentityUserRole : IdentityUserRole<string> { public virtual ApplicationUser User { get; set; } public virtual ApplicationRole Role { get; set; } } public class ApplicationRole : IdentityRole {

How to deploy a code-first Entity Framework database using Azure Devops pipelines to SQL server

烈酒焚心 提交于 2020-01-01 12:20:36
问题 We are building an ASP.net application using Entity Framework core with Azure DevOps and targeting IIS with MSSQL server 2016. Our test server is a local Windows 2016 machine containing both IIS and the SQL server instance. I have successfully written a deployment workflow enabling continuous deployment on that test server of the code itself but I can't find any way to deploy the database. The first version was targeting asp.net core 2.0 so I could use the command-line to start the

Use LinqKit PredicateBuilder for related model (EF Core)

℡╲_俬逩灬. 提交于 2020-01-01 09:12:20
问题 I want to use LinqKit's PredicateBuilder and pass the predicate into .Any method for related model. So I want to build a predicate: var castCondition = PredicateBuilder.New<CastInfo>(true); if (movies != null && movies.Length > 0) { castCondition = castCondition.And(c => movies.Contains(c.MovieId)); } if (roleType > 0) { castCondition = castCondition.And(c => c.RoleId == roleType); } And then use it to filter model that has relation to model in predicate: IQueryable<Name> result = _context

Entity Framework Core Code-First: Cascade delete on a many-to-many relationship

血红的双手。 提交于 2020-01-01 08:55:53
问题 I'm working on an ASP.NET MVC 6 project with Entity-Framework Core (version "EntityFramework.Core": "7.0.0-rc1-final" ) backed by a SQL Server 2012 express DB. I need to model a many-to-many relationship between a Person entity and an Address entity. As per this guide I modeled it with a PersonAddress join-table entity, because this way I can store some extra info. My goal is to set-up my system this way: If a Person instance is deleted, all the related PersonAddress instances must be deleted

How to query many-to-many releationship in EF Core

余生长醉 提交于 2020-01-01 08:43:24
问题 I'm using .NET Core and EF Core for a web project. I'm struggling how to query a many-to-many releationship. This is what my models look like: public class Begrip { public int ID { get; set; } public string Name { get; set; } public string Desc { get; set; } [Url] public string URL { get; set; } public ICollection<BegripCategory> Categories { get; set; } } public class Category { public int ID { get; set; } public string Name { get; set; } public ICollection<BegripCategory> Begrippen { get;