entity-framework-core

EF Core Entity Circular Reference

天涯浪子 提交于 2019-12-11 04:06:26
问题 I am using C#.net and ef core. I have the models below. When I get my list of competitions, I want to only get my related user. However, I am getting the User and all the User's competitions. How can I achieve this? I had to do the following to get my list of competitions to show: .AddJsonOptions(opt => opt.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore) public partial class Competition { public int CompetitionId { get; set; } public int UserId { get;

Converting IConfigurationSection to IOptions

我怕爱的太早我们不能终老 提交于 2019-12-11 03:56:51
问题 The Options pattern allowed me to create options objects containing values from configuration, as described here: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/options?view=aspnetcore-2.1 I need the values for one option object within an IDesignTimeDbContextFactory implementation, to be used by EF when creating the models. (The values in that config section will be used for data seeding, and so I added IOptions to the DB Context constructor.) As I have no access to

Value Object as Invalid Object in asp.net core 2.1

谁都会走 提交于 2019-12-11 03:49:26
问题 I have been using value object in asp.net core 2.0 project, which was running properly on that project. I updated the project to 2.1 and it is giving me an error as Invalid object name 'EntityAdress'. Entity: public class Company : AuditableEntity<long> { public int SalesRepId { get; set; } public string Name { get; set; } public int StatusId { get; set; } public EntityAdress Addresses { get; set; } public string BillingAddress { get; set; } } public class EntityAdress : ValueObject { private

How can I prevent EF7 from eagerly fixing up navigation properties?

给你一囗甜甜゛ 提交于 2019-12-11 03:39:34
问题 I have an issue using EF7 in a web application with which I could use some help. I'm currently using EF7 RC1. Here are some models that illustrate my problem. Contact public class Contact { public Guid Id { get; set; } public string Desc { get; set; } public ContactType ContactType { get; set; } } ContactType public class ContactType { public Guid Id { get; set; } public string Desc { get; set; } public ICollection<Contact> Contacts { get; set; } } These models are related via Fluent API like

Configuring cascade delete with EF7

孤人 提交于 2019-12-11 03:25:48
问题 I'm trying to configure a cascade delete using EF7 in a ASP.NET vNext application but I cannot find how to do it. I'm overriding OnModelCreating and I have this code: modelBuilder.Entity<Category>() .OneToMany<Category>( e => e.SubCategories ) .ForeignKey( e => e.ParentCategoryId ); The generated migration looks like this: migrationBuilder.AddForeignKey("Category", "FK_Category_Category_ParentCategoryId", new[] { "ParentCategoryId" }, "Category", new[] { "Id" }, cascadeDelete: false); Anyone

Entity Framework Core Mutiple DbContext Migrations in same database

折月煮酒 提交于 2019-12-11 02:51:59
问题 In Entity Framework 6 you could specify a ContextKey for a DbContext so that when using one database for multiple DbContexts the Migrations table was aware of what migration targeted what DbContext. I've spent the last few hours trying to work out how you achieve the same in Entity Framework Core but haven't worked it out and documentation is lacking. The new Migrations table in EF Core only has two columns - MigrationId, ProductVersion. I thought HasDefaultSchema was the answer but doesn't

Raise async event in EF's DbContext.SaveChangesAsync()

天涯浪子 提交于 2019-12-11 02:38:05
问题 I'm using EF Core, in an ASP.NET Core environment. My context is registered in my DI container as per-request. I need to perform extra work before the context's SaveChanges() or SaveChangesAsync() , such as validation, auditing, dispatching notifications, etc. Some of that work is sync, and some is async. So I want to raise a sync or async event to allow listeners do extra work, block until they are done (!), and then call the DbContext base class to actually save. public class MyContext :

Either get local entity or attach a new one

隐身守侯 提交于 2019-12-11 02:35:12
问题 My Entity looks like this: public class User { public int Id {get; set;} } I don't want to query the database each time I have get a specific User where I know a User exists for this Id. Seems like Attach works for this case but If the DbContext already stores the Entity for this specific User locally It will throw an exception. Example what I want to do: var user1 = ctx.GetLocalOrAttach(new User{Id = 1}); var user2 = ctx.GetLocalOrAttach(new User{Id = 2}); AddUserRelation(user1, user2); Is

EF Core custom count query

我只是一个虾纸丫 提交于 2019-12-11 02:26:26
问题 I'm working on a small ASP.NET Core project for tagging images using Entity Framework Core on a Sqlite database, mainly just for learning. There are two tables (and POCOs), Tags and Images, where multiple tags are related to each image. I'm trying to get a count of all Images that have tags associated with them. In plain SQL I'd write SELECT COUNT(DISTINCT ImageId) FROM Tags to get the count, and in LINQ I came up with _context.Tags.Select(t => t.Image).Distinct().Count() . But that LINQ

Entity Framework Core 1.0.1 add-migration

人盡茶涼 提交于 2019-12-11 02:26:11
问题 Since EF Core migrated to Microsoft.EntityFrameworkCore.* (instead of EntityFramework.* ), it seems that the recommended command line to add db migrations is back to add-migration from package manager. ef core add migration documentation However it seems there isn't a package anymore for commands and that the package installation of v 1.0.1 doesn't add any command. The error message I'm getting when running Add-Migration is Cannot execute this command because EntityFramework.Commands is not