EF7,我的关系出了什么问题?

时间:2016-02-25 09:43:43

标签: asp.net entity-framework

关系接缝可以找到并与其他示例一起使用但是当试图获得所有可能的包含时,它会因错误而失败。

 Widget widget = _dbContext.Widgets.Where(w => w.Id == id)
            .Include(w => w.DataSource)
                .ThenInclude(d => d.DataSourceParams)
                    .ThenInclude(p => p.DataSourceParamProc)
                       .ThenInclude(s => s.DataSourceParamProcParams)
            .Include(w => w.DataSource)
                .ThenInclude(d => d.DataSourceParams)
                    .ThenInclude(p => p.SelectOptions)
            .FirstOrDefault();

POCO' S:

public class Widget
{
    public int Id { get; set; }
    public string Title { get; set; }
    public int DataSourceId { get; set; }
    public string Colour { get; set; }
    public string Icon { get; set; }
    public virtual DataSource DataSource { get; set; }
}

public class DataSource
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public string StoredProc { get; set; }
    public string DataBaseName { get; set; }
    public virtual ICollection<DataSourceParam> DataSourceParams { get; set; }
}

public class DataSourceParam
{
    public int Id { get; set; }
    public int DataSourceId { get; set; }
    public string Name { get; set; }
    public int TypeId { get; set; }

    public virtual DataSource DataSource { get; set; }
    public virtual DataSourceParamProc DataSourceParamProc { get; set; }
}

public class DataSourceParamProc
{
    public int Id { get; set; }
    public string StoredProc { get; set; }
    public string DataBaseName { get; set; }
    [ForeignKey("DataSourceParam")]
    public int DataSourceParamId { get; set; }
    public virtual DataSourceParam DataSourceParam { get; set; }
    public virtual ICollection<DataSourceParamProcParam> DataSourceParamProcParams { get; set; }
}

public class DataSourceParamProcParam
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string DefaultValue { get; set; }
    public int DataSourceParamProcId { get; set; }
}

public class SelectOption
{
    public int Id { get; set; }
    public string Value { get; set; }
    public string Label { get; set; }
    public int DataSourceParamId { get; set; }
    public virtual DataSourceParam DataSourceParam { get; set; }
}

我收到错误:

  

已按列表顺序多次指定列。按列表排列的列必须是唯一的。

如果我对此行发表评论,则可行:

Widget widget = _dbContext.Widgets.Where(w => w.Id == id)
            .Include(w => w.DataSource)
                .ThenInclude(d => d.DataSourceParams)
                    .ThenInclude(p => p.DataSourceParamProc)
                       //.ThenInclude(s => s.DataSourceParamProcParams)
            .Include(w => w.DataSource)
                .ThenInclude(d => d.DataSourceParams)
                    .ThenInclude(p => p.SelectOptions)
            .FirstOrDefault();

或者,如果我评论这个,它也有效:

Widget widget = _dbContext.Widgets.Where(w => w.Id == id)
            .Include(w => w.DataSource)
                .ThenInclude(d => d.DataSourceParams)
                    .ThenInclude(p => p.DataSourceParamProc)
                       .ThenInclude(s => s.DataSourceParamProcParams)
            //.Include(w => w.DataSource)
            //    .ThenInclude(d => d.DataSourceParams)
            //        .ThenInclude(p => p.SelectOptions)
            .FirstOrDefault();

如果我将其分开,它也会起作用:

       Widget widget = _dbContext.Widgets.Where(w => w.Id == id)
            .Include(w => w.ChartAttributes)
                .ThenInclude(c => c.ChartAttributeType)  
            .Include(w => w.WidgetCategory)
            .Include(w => w.WidgetSeries)
            .Include(w => w.WidgetType)
            .FirstOrDefault();

        DataSource ds = _dbContext.DataSources.Where(d => d.Id == widget.DataSourceId)
                .Include(d => d.DataSourceParams)
                    .ThenInclude(p => p.SelectOptions)
                .Include(d => d.DataSourceParams)
                    .ThenInclude(p => p.DataSourceParamProc)
                       .ThenInclude(s => s.DataSourceParamProcParams).FirstOrDefault();

        widget.DataSource = ds;

0 个答案:

没有答案