Tag: 实体 框架 核心

Include()ThenInclude()在Table Per Hierarchy策略中抛出“Sequence包含多个匹配元素”exception

我正在使用Entity Framework 7和代码优先,我有一个涉及3个级别的父子关系的模型: Corporations有companies Companies属于corporation并拥有factories Factories属于一家company 由于这3个实体共享很多共同点,因此它们都从一个抽象的BaseOrganization实体inheritance。 当我试图列出所有工厂,包括他们的母公司,然后包括他们的母公司时,我有这两种不同的情况: 如果不将 BaseOrganization 包含到上下文中,代码优先创建三个表 (对应于Table-Per-Concrete-Type或TPC模式)。 Include()和ThenInclude()工作正常,我可以按预期列出工厂和遍历关系。 将 BaseOrganization 包含到上下文中,代码优先创建一个带有鉴别器字段的表 (对应于Table-Per-Hierarchy或TPH模式)。 Include()和ThenInclude()抛出一个Sequence contains more than one matching elementexception的Sequence contains more than one matching element 。 这个问题(没有inheritance和抽象基类模式)已经在EF7 Github repo中得到了解决,并且已被清除(参见https://github.com/aspnet/EntityFramework/issues/1460 )。 所以我目前不知道我的方法是否有问题,或者这显然是EF7 RC1的问题? 请注意,我更喜欢保持inheritance,以便我的SQL模型更具可读性。 以下是完整的复制代码: using System; using System.Collections.Generic; using System.Linq; using Microsoft.Data.Entity; namespace MultiLevelTest { // All places share name […]

entity framework7审核日志

我正在将一个旧项目移植到ASP.NET 5和Entity Framework 7.我使用数据库第一种方法(DNX scaffold)来创建模型。 旧项目基于entity framework4,审计跟踪是通过覆盖DbContext的SaveChanges方法实现的: public override int SaveChanges(System.Data.Objects.SaveOptions options) { int? UserId = null; if (System.Web.HttpContext.Current != null) UserId = (from user in Users.Where(u => u.UserName == System.Web.HttpContext.Current.User.Identity.Name) select user.Id).SingleOrDefault(); foreach (ObjectStateEntry entry in ObjectStateManager.GetObjectStateEntries(EntityState.Added | EntityState.Modified)) { Type EntityType = entry.Entity.GetType(); PropertyInfo pCreated = EntityType.GetProperty(“Created”); PropertyInfo pCreatedById = EntityType.GetProperty(“CreatedById”); PropertyInfo pModified […]

导航属性应该是虚拟的 – 在ef核心中不需要?

我记得在EF 导航属性应该是虚拟的 : public class Blog { public int BlogId { get; set; } public string Name { get; set; } public string Url { get; set; } public string Tags { get; set; } public virtual ICollection Posts { get; set; } } 但我看看EF Core并不认为它是虚拟的: public class Student { public int ID { get; […]