ef-core-2.0

Check if applied migrations match the DbContext?

跟風遠走 提交于 2020-05-13 02:42:51
问题 I want to create a unit test to ensure no developer will commit model changes without the corresponding migration. How do I test that the database matches the DbContext? 回答1: You can leverage some of the lower-level Migrations components to do that: var migrationsAssembly = db.GetService<IMigrationsAssembly>(); var differ = db.GetService<IMigrationsModelDiffer>(); var hasDifferences = differ.HasDifferences( migrationsAssembly.ModelSnapshot.Model, db.Model); Assert.False(hasDifferences, "You

Check if applied migrations match the DbContext?

99封情书 提交于 2020-05-13 02:42:10
问题 I want to create a unit test to ensure no developer will commit model changes without the corresponding migration. How do I test that the database matches the DbContext? 回答1: You can leverage some of the lower-level Migrations components to do that: var migrationsAssembly = db.GetService<IMigrationsAssembly>(); var differ = db.GetService<IMigrationsModelDiffer>(); var hasDifferences = differ.HasDifferences( migrationsAssembly.ModelSnapshot.Model, db.Model); Assert.False(hasDifferences, "You

Use IEntityTypeConfiguration with a base entity

╄→гoц情女王★ 提交于 2020-04-07 19:07:50
问题 In EF Core 2.0, we have the ability to derive from IEntityTypeConfiguration for cleaner Fluent API mappings (source). How can I extend this pattern to utilize a base entity? In the example below, how can I have a BaseEntityConfiguration to reduce duplication in LanguageConfiguration and MaintainerConfiguration , modifying properties that are in the BaseEntity only in the BaseEntityConfiguration ? What would such a BaseEntityConfiguration look like; and how would it be used, if at all, in

Use IEntityTypeConfiguration with a base entity

廉价感情. 提交于 2020-04-07 19:07:49
问题 In EF Core 2.0, we have the ability to derive from IEntityTypeConfiguration for cleaner Fluent API mappings (source). How can I extend this pattern to utilize a base entity? In the example below, how can I have a BaseEntityConfiguration to reduce duplication in LanguageConfiguration and MaintainerConfiguration , modifying properties that are in the BaseEntity only in the BaseEntityConfiguration ? What would such a BaseEntityConfiguration look like; and how would it be used, if at all, in

EF Core LINQ exclude column from included entity

百般思念 提交于 2020-04-07 06:38:52
问题 I'm using ASP.Net Core 2.0 with Entity Framework and I am trying to return a model to a page that contains the Employment entity with it's collection of EmploymentDocument entities also included. For the latter I do not want to load the data (byte[]) column but I do want all the other columns, most importantly FileName. The linq query I have which loads everything including the data column is: var employment = await _context.Employment .Include(e => e.EmploymentDocuments) // will load all

EF Core 2.1 slow startup

烂漫一生 提交于 2020-02-02 06:47:42
问题 After having some experience with EF6 (like this), I wanted to try out EF Core, because I've read some articles and watched some videos saying that it outperforms EF6 by a very large margin. So, I made sample program with class: public interface IEntity { int Id { get; set; } } public class Employee : IEntity { public int Id { get; set; } public string FirstName { get; set; } public string Surname { get; set; } } Then I created a repository pattern with generic repository: public interface

EF Core Table Splitting - One table to multiple classes

不想你离开。 提交于 2020-01-24 00:30:10
问题 My table has four columns and I want to split it between multiple classes. table1 key col1 col2 col3 col4 Class ClassA key col1 col2 class ClassB key col3 col4 modelBuilder.Entity().ToTable("table1"); modelBuilder.Entity().ToTable("table1"); Currently it give me System.InvalidOperationException: 'Cannot use table 'table1' for entity type 'ClassB' since it is being used for entity type 'ClassA' Is it possible in EF Core? Thanks 回答1: You may need a relationship defined like the following based

using EF Core IdentityContext and DbContext both for order management

这一生的挚爱 提交于 2020-01-14 19:17:28
问题 I am working on creating an ecommerce website on ASP MVC Core 2. I inherited my user from IdentityUser and inherited context from IdentityDbContext for working with user data and inherited a different context from DbContext for working with products and orders etc. Now, I want to link an order or shopping cart to a particular user and can not wrap my head around how to refer to the user in order table as they are in different contexts. I am also using the default guid created by EF as primary

EF Core Creating Object with “infinite” depth

无人久伴 提交于 2020-01-06 05:36:07
问题 I have an M to N relationship on my Database. I took the sample Database "sakila" from the MySql page. I have set up my Model Objects as follows. Film Table public partial class Film { public Film() { FilmActor = new HashSet<FilmActor>(); FilmCategory = new HashSet<FilmCategory>(); Inventory = new HashSet<Inventory>(); } //Some Properties public ICollection<Inventory> Inventory { get; set; } } Linking Table Inventory public partial class Inventory { public Inventory() { Rental = new HashSet

EF core Linq groupby and having sum count - could not be translated and will be evaluated locally

孤街浪徒 提交于 2020-01-04 09:08:28
问题 Following .net core EF core, Linq cannot be translated and will be evaluated locally. Can you please give me an advise? var temp1= (from so in context.OrderShippingOrders group so by so.OrderId into g where g.Count(x=> x.IsSent == true ) == g.Count() select new { g.Key } ); query = (from o in context.Orders join s in temp1 on o.Id equals s.Key select o ); The LINQ expression 'join AnonymousObject _o in {from Order o in value(Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable 1