entity-framework-core

Seeding many-to-many databases in EFCore5 with ModelBuilder?

送分小仙女□ 提交于 2020-12-12 21:55:57
问题 There are many questions about seeding many-to-many relationships in Entity Framework. However, most of them are extremely old, and many-to-many behavior has changed significantly in EFCore5. The official docs recommend overriding OnModelCreating to implement ModelBuilder.Entity<>.HasData() . However, with the new many-to-many behavior (without explicit mappings), I can find no clear path to seed the intermediate tables. To use the example of this tutorial, the BookCategories class is now

Seeding many-to-many databases in EFCore5 with ModelBuilder?

喜欢而已 提交于 2020-12-12 21:50:51
问题 There are many questions about seeding many-to-many relationships in Entity Framework. However, most of them are extremely old, and many-to-many behavior has changed significantly in EFCore5. The official docs recommend overriding OnModelCreating to implement ModelBuilder.Entity<>.HasData() . However, with the new many-to-many behavior (without explicit mappings), I can find no clear path to seed the intermediate tables. To use the example of this tutorial, the BookCategories class is now

Unable to create an object of type 'MyEFCoreDbContext' xaf Blazor EFCore

不问归期 提交于 2020-12-12 11:47:13
问题 I created a new project with the XAF Blazor 20.2.3 wizard and upgraded the EFCore packages to 5.0. In PM I ran Install-Package Microsoft.EntityFrameworkCore.Tools However when I run add-migration initial I get Unable to create an object of type 'ExamBuddyEFCoreDbContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728 I have looked at the link but I don't think it applies to the xaf Blazor app. I tried commenting out the Database

EF Core 5 with Sum

主宰稳场 提交于 2020-12-12 05:40:11
问题 Repost model class: public class TransactionDayReport { public Decimal TotalAmount { get; set; } public ICollection<TransactionResponse> TransactionResponses { get; set; } = null!; } This is my repository method public async Task<IEnumerable<Transaction>> SearchTransactionAsync(SearchTransaction search) { var query = _context.Transactions .Include(t => t.Branch) .Include(t => t.Customer) .AsQueryable(); if (search.CIN != null) query = query.Where(t => t.Customer.CIN.Contains(search.CIN)); if

EF Core 5 with Sum

岁酱吖の 提交于 2020-12-12 05:39:57
问题 Repost model class: public class TransactionDayReport { public Decimal TotalAmount { get; set; } public ICollection<TransactionResponse> TransactionResponses { get; set; } = null!; } This is my repository method public async Task<IEnumerable<Transaction>> SearchTransactionAsync(SearchTransaction search) { var query = _context.Transactions .Include(t => t.Branch) .Include(t => t.Customer) .AsQueryable(); if (search.CIN != null) query = query.Where(t => t.Customer.CIN.Contains(search.CIN)); if

How do I store a property of IEnumerable<string> in a Cosmos table, with EF Core 3.1

青春壹個敷衍的年華 提交于 2020-12-12 04:01:34
问题 My project is using EF Core 3.1, and targeting Azure Cosmos as the database. I have an entity like this: public class MyEntity { public IEnumerable<string> Names {get;set;} ... other fields } That I would like to end up in a Cosmos document like this: { "Names": ["Name1", "Name2"] } With the entity as is ( IEnumerable<string> ) I get the error: The property 'MyEntity.Names' could not be mapped, because it is of type 'IEnumerable' which is not a supported primitive type or a valid entity type.

C# Linq Expression could not be Translated

别来无恙 提交于 2020-12-12 03:59:13
问题 I am trying to execute a linq query to get all employes that have some specific skills. search.skills is a list of strings with some skills and I want to retrieve all the user which have all those skills (And condition). In my where clause on employees, exp.Skills is ICollection and expSkill.SkillName is the skill name .Where( emp => search.Skills.All( searchSkill => emp.Experiences.Select(exp => exp.Skills).SelectMany(x => x).Select(expSkill => expSkill.SkillName).Contains(searchSkill) ))

C# Linq Expression could not be Translated

≡放荡痞女 提交于 2020-12-12 03:59:12
问题 I am trying to execute a linq query to get all employes that have some specific skills. search.skills is a list of strings with some skills and I want to retrieve all the user which have all those skills (And condition). In my where clause on employees, exp.Skills is ICollection and expSkill.SkillName is the skill name .Where( emp => search.Skills.All( searchSkill => emp.Experiences.Select(exp => exp.Skills).SelectMany(x => x).Select(expSkill => expSkill.SkillName).Contains(searchSkill) ))

Entity Framework Core Two Objects as Primary Key

余生颓废 提交于 2020-12-10 06:13:24
问题 I have a model which is used for managing friend relationships. It looks as follows: public class Relationship { [Required] public User User { get; set; } [Required] public User Friend { get; set; } [Required] public DateTimeOffset RelationshipInitializationDate { get; set; } } Users will have multiple records for their ID and there will be multiple records with the same FriendID so defining either of these as a key is a no-go. I would like the key to be a composite between User and Friend

How to get Inner Join using navigation property in Entity Framework Core

余生长醉 提交于 2020-12-06 16:27:12
问题 I want to generate a query that uses an inner join for a one to one relationship using a navigation property. This is for an existing database that I cannot change. Here are what my entities look like: [Table("Info")] public class Info { [Key] public int InfoId { get; set; } public string Name { get; set; } public virtual MoreInfo MoreInfo { get; set; } } [Table("MoreInfo")] public class MoreInfo { [Key] public int InfoId { get; set; } public string OtherName { get; set; } public int