EF Core:加载相关实体——循环依赖

Dan*_*ish 4 entity-framework-core asp.net-core-mvc

我在 EF Core 中有 2 个相关实体(数据库首先从现有数据库设计)并且在加载一对多关系时遇到问题 - 它是一个 webapi ASP.NET core 1.0 应用程序

品牌实体

 [Table("tblBranding")]
public class Brand {
    [Key]
    [Column("brandingId")]
    public int BrandId { get; set; }
    [Column("BrandingActive")]
    public bool Active { get; set; }
    [JsonIgnore]
    [Column("DeadBrand")]        
    public bool DeadBrand { get; set; }
    [Column("BrandingSiteTitle")]
    public string Name { get; set; }

    //navigation properties
    public virtual ICollection<Event> Events { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

事件实体:

[Table("tblEvents")]
public class Event
{   
    public int EventId { get; set; }
    [Column("eventActive")]
    public bool Active { get; set; }
    [Column("eventName")]
    public string Name { get; set; }        
    public DateTime EventCloseDate {get;set;}        
    public int PaxAllocationLimit { get; set; }

    //navigation properties                
    [Column("BrandingId")]
    public int BrandId { get; set; }        
    public virtual Brand Brand { get; set; }
    public virtual ICollection<Session> Sessions { get; set; }

}
Run Code Online (Sandbox Code Playgroud)

来自 DbContext 中 OnModelCreating 中 FLUID API 的代码:

modelBuilder.Entity<Event>()
            .HasOne(e => e.Brand)
            .WithMany(b => b.Events).HasForeignKey(e=>e.BrandId);

    public virtual DbSet<Brand> Brands { get; set; }
    public virtual DbSet<Event> Events { get; set; }  
Run Code Online (Sandbox Code Playgroud)

BrandsController 的代码:

    [HttpGet]
    public IActionResult Get() {
        //var brands = from b in _context.Brands
        //             where b.Active == true
        //             orderby b.BrandName
        //             select b;

        var brands = _context.Brands.Include(e => e.Events).Where(b => b.Active == true).OrderBy(b => b.Name);

        return new ObjectResult(brands);
    }   
Run Code Online (Sandbox Code Playgroud)

来自 EventsController 的代码

 // GET: api/values
    [HttpGet("{id:int?}")]
    public IActionResult Get(int? id) {
        var events = from e in _context.Events
                     where e.Active == true
                     orderby e.Name
                     select e;

        if (!events.Any()) {
            return HttpNotFound();
        }

        if (id != null) {
            events = events.Where(e => e.EventId == id).OrderBy(e => 0);
            if (events.Count() == 0) { return HttpNotFound(); }
            return new ObjectResult(events);
        }
        else {
            return new ObjectResult(events);
        }
    }
Run Code Online (Sandbox Code Playgroud)

当我尝试通过 API 加载品牌时,出现异常:

Microsoft.Data.Entity.Storage.Internal.RelationalCommandBuilderFactory:信息:已执行的 DbCommand (80ms) [Parameters=[], CommandType='Text', CommandTimeout='30'] SELECT [t].[EventId], [t]。 [EventCloseDate], [t].[eventActive], [t].[BrandingId], [t].[EventId1], [t].[eventName], [t].[PaxAllocationLimit] FROM [tblEvents] AS [t ] INNER JOIN ( SELECT DISTINCT [e].[BrandingSiteTitle], [e].[brandingId] FROM [tblBranding] AS [e] WHERE [e].[BrandingActive] = 1) AS [e] ON [t].[ BrandingId] = [e].[brandingId] ORDER BY [e].[BrandingSiteTitle], [e].[brandingId] Microsoft.Data.Entity.Query.Internal.SqlServerQueryCompilationContextFactory:错误:迭代数据库时发生异常查询的结果。System.Data.SqlClient.SqlException (0x80131904):无效的列名“EventId1”。

除此之外,有没有办法在不使用“包含”的情况下加载相关实体?如果我不使用包含并使用标准 LINQ 查询,相关实体将加载为 NULL

更新

我现在收到一个无效的列错误 - 我在之前的代码中注意到我没有在品牌的 ICollection 上使用 virtual

现在我不知道为什么它会在 SQL 中生成 EventId1 列

EF 7 版本是 1.0.0-rc1-final

UPDATE-2 在使用代码后,异常更改为代码中的循环依赖异常,给出与上述完全相同的代码 - 我不知道为什么它之前生成了无效的列名(EventId1)

Dan*_*ish 6

在这里回答我自己的问题 - 想通了 -

此处使用的 2 个实体,我在 EF 7 中使用了完全定义的关系 - 但是 JSON 序列化程序不喜欢那样,因为这会创建循环依赖 - 品牌包含事件列表,并且每个事件还包含父品牌属性 -

所以这里的解决方案是将 [JsonIgnore] 属性添加到孩子的关系属性中

更新事件类:

[Table("tblEvents")]
public class Event
{   
    public int EventId { get; set; }
    [Column("eventActive")]
    public bool Active { get; set; }
    [Column("eventName")]
    public string Name { get; set; }        
    public DateTime EventCloseDate {get;set;}        
    public int PaxAllocationLimit { get; set; }

    //navigation properties             
    [JsonIgnore]
    [Column("brandingId")]
    public virtual int BrandId { get; set; }
    [JsonIgnore]
    public virtual Brand Brand { get; set; }
    //public virtual ICollection<Session> Sessions { get; set; }

}
Run Code Online (Sandbox Code Playgroud)

  • 好吧,您不必`[JsonIgnore]` ID,但您必须忽略子对象和/或子列表对象 (2认同)