entity-framework-core

Entityframework core 2.0 how to map Many-to-Many without creating a join table

試著忘記壹切 提交于 2019-12-20 07:32:13
问题 I am trying this activity to clone from existing .edmx project to code first. I have two entities. I want to have many to many relation without creating a new table. I am using EF Core 2.0 code first approach. Please find below the entity i have created. I am not sure whether is this is the right way to do this. I would like to have the foreign key column on both the tables ie. WorkflowId and WorkCaseId on WorkCase and Workflow tables respectively. public class WorkCase { [Key] public int

Entity Framework insert conflicts with another foreign key

泄露秘密 提交于 2019-12-20 06:32:42
问题 I am having difficulty inserting data with its related entities. public class Status : Entity, IAggregateRoot { //other properties public readonly List<Video> _videos; public readonly List<Photo> _photos; } -- public class Log : Entity, IAggregateRoot { //other properties public readonly List<Video> _videos; public readonly List<Photo> _photos; public readonly List<Sound> _audios; } -- public class Photo : Entity, IAggregateRoot { //other properties public string Type { get; set; } public int

EF Core defeats the purpose of constructor with null checking

限于喜欢 提交于 2019-12-20 06:06:53
问题 I have designed my models so that it should not be created with a null parameter. For example, if I want to enforce that every Post should have a corresponding Blog , then my models will look like: public class Post { private Post() { } public Post(Blog blog) { Blog = blog ?? throw new ArgumentNullException(nameof(blog)); } public int PostId { get; private set; } public Blog Blog { get; private set; } } public class Blog { public int BlogId { get; private set; } } This way, it will throw an

Group and Select First From Two Tables Linq

时光毁灭记忆、已成空白 提交于 2019-12-20 04:38:43
问题 I'm trying to create a simple query using EFCore, returning the list of people i'm conversing with, and the last message that was sent between the two of us (pretty much like how it's displayed on Facebook Messenger or Whatsapp). I created the linq query but its generating one hell of an sql query. I'm trying to optimize the linq query to generate a better sql, so here comes the full story: 0. The Two Entities: Visitors and ChatMessages The Visitor contains the visitor information, and the

Avoid Exposing Private Collection Properties to Entity Framework. DDD principles

一个人想着一个人 提交于 2019-12-20 03:49:10
问题 I try do adhere DDD principles on C# collections see more here And I notice that the model builder method for initial seed HasData relies on the Add Method of ICollection . There is any way to circumvent or trick that method when is called from database update / migrate process? All I had done until now to trick it follows this path. 1) Create a wrapper around the ICollection named ReadOnlyKollection 2) Have a private ICollection on the model, to avoid exposing to the outside world the

Optional Parameters with EF Core FromSql

倖福魔咒の 提交于 2019-12-20 03:43:11
问题 Is it possible to use EF Core FromSql to execute a stored procedure that has optional parameters? I have been testing out a simple scenario to use as a template for updating old EF6 calls to EF Core calls. The test example I am using as a proof of concept: CREATE PROCEDURE [dbo].[TestNullableParameters] @addressId int = null, @city nvarchar(100) = null, @createdByUserId int AS BEGIN select * from CRM..Address a where (@addressId is null or a.AddressId = @addressId) and (@city is null or a

Entity Framework Core 2.1 failing to translate query properly

爱⌒轻易说出口 提交于 2019-12-20 03:33:18
问题 I have an existing database which I am accessing from 2 separate projects, one an ASP.NET MVC 5 project, and one running .NET Core 2.1 using the respective Entity Framework verisons in each. My problem is that the query that I'm using on the MVC project is being translated incorrectly on the .NET Core project My 2 models are as follows, a Job which has 0 or more Workorders: public virtual DbSet<Job> Jobs { get; set; } public virtual DbSet<WorkOrder> WorkOrders { get; set; } public class Job {

Generate a composite unique constraint/index with owned entity, in EF Core

我只是一个虾纸丫 提交于 2019-12-20 02:57:05
问题 I have an entity that owns another entity public class Entity1 { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public virtual int ID { get; set; } public string Property { get; set; } public Entity2 Description { get; set; } } public class Entity2 { public string Test { get; set; } } and I need to create an index on Entity1.Property and Entity2.Test. The configuration is like this builder.OwnsOne(pt => pt.Description); builder.HasIndex(p => new { p.Property, p.Description.Test }

EF7 beta6: Error saving multiple entities

寵の児 提交于 2019-12-20 02:45:30
问题 I am creating an API using ASP.NET5 and Entity Framework 7.0.0-beta 6, and when I try to execute various updates in several requests, I get this Exception: 'Company' cannot be tracked because another instance of this type with the same key is already being tracked. For new entities consider using an IIdentityGenerator to generate unique key values. This is my code: public class MrBellhopContext : DbContext { public DbSet<Company> Company { get; set; } protected override void OnModelCreating

EF Core 3.0 SumAsync triggers aggregate function exception

杀马特。学长 韩版系。学妹 提交于 2019-12-20 02:30:36
问题 I am in the process of upgrading to EF Core 3.0 and .NET Core 3.0, but some of my queries stopped working. Here is an example: I have a table called Bins , I have another table which is called BinItems , now it has, of course, a one to many relationship. BinItems has a property called Qty , and I want to sum up all the Qty from BinItems based on criteria given by the client in a filter. So here is the code: var query = _binRepository.Table; if (filter.LastRecountDate != null) { query = query