Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc ASP.NET MVC列表连接父表字段_Asp.net Mvc_Entity Framework - Fatal编程技术网

Asp.net mvc ASP.NET MVC列表连接父表字段

Asp.net mvc ASP.NET MVC列表连接父表字段,asp.net-mvc,entity-framework,Asp.net Mvc,Entity Framework,这有点尴尬,因为我相信答案很简单 我正在使用Entityframework和代码优先技术尝试构建我的第一个功能性MVC表单,同时遵循优秀的ASP.NET教程 如何向正在查询的表显示包含父级信息的字符串。我想在下拉列表中使用的字符串中包含一个父值。。。。或者我应该以相反的方式选择父对象,并让子对象作为选择的结果出现 我认为这就像添加到模型中一样简单,因为它已经在与其父对象进行对话。Intellisense对此没问题:-) 模范班 public class SourceLocation {

这有点尴尬,因为我相信答案很简单

我正在使用Entityframework和代码优先技术尝试构建我的第一个功能性MVC表单,同时遵循优秀的ASP.NET教程

如何向正在查询的表显示包含父级信息的字符串。我想在下拉列表中使用的字符串中包含一个父值。。。。或者我应该以相反的方式选择父对象,并让子对象作为选择的结果出现

我认为这就像添加到模型中一样简单,因为它已经在与其父对象进行对话。Intellisense对此没问题:-)

模范班

public class SourceLocation
{
    [Key]
    public int SourceLocationID { get; set; }
    public int SourceID { get; set; }

    [Required]
    [Display(Name = "Product Type")]
    [StringLength(25)]
    public string ProductType { get; set; }

    [Required]
    [Display(Name = "Source Location")]
    [StringLength(50)]
    public string SamplingLocation { get; set; }
    [Display(Name = "Sampling Location Notes")]
    public string LocationNotes { get; set; }
    public string SourceProductType
    {
         get
        {
            return CementSources.SampleSource + " " +  ProductType + " ex " + SamplingLocation;
        }        
}

    public virtual CementSource CementSources { get; set; }
}
}

引用SourceSampleType的控制器配置如下

// GET: Specifications/Create
        public ActionResult Create()
        {
            ViewBag.FieldID = new SelectList(db.Fields, "FieldID", "FieldName");
            ViewBag.SourceLocationID = new SelectList(db.SourceLocations, "SourceLocationID", "SourceProductType");
            ViewBag.SpecificationTypeID = new SelectList(db.SpecificationTypes, "SpecificationTypeID", "SpecificationTypeName");
            return View();
        }
当我尝试创建配置为显示SourceSampleType的新样本时,错误为:


已存在与此命令关联的打开的DataReader,必须先关闭该命令。 描述:执行当前web请求期间发生未处理的异常。请查看堆栈跟踪以了解有关错误的更多信息以及错误在代码中的起源

异常详细信息:System.InvalidOperationException:已存在与此命令关联的打开的DataReader,必须先关闭该DataReader

源错误:

Line 28:             get
Line 29:             {
Line 30:                 return CementSources.SampleSource + " " +  ProductType + " ex " + SamplingLocation;
Line 31:             }
Line 32:         }


我是否没有正确使用此语法,或者这与我尚未解析和理解的急/缓加载有关?

发生错误的原因是您迭代了
SourceLocation
的结果,但在每次迭代中,您都会执行另一个查询以获取其
cencources
属性的值

您需要从模型中删除
SourceProductType
属性,并在查询中使用
Include()
来包含
cencources

ViewBag.SourceLocationID = db.SourceLocations
    .Include(x => x.CementSources)
    .Select(x => new SelectListItem
    {
        Value = x.SourceLocationID.ToString(),
        Text = string.Format("{0} {1} ex {2}", x.CementSources.SampleSource, x.ProductType, x.SamplingLocation)
    });

出现此错误的原因是您迭代了
SourceLocation
的结果,但在每次迭代中,您都会执行另一个查询以获取其
cencources
属性的值

您需要从模型中删除
SourceProductType
属性,并在查询中使用
Include()
来包含
cencources

ViewBag.SourceLocationID = db.SourceLocations
    .Include(x => x.CementSources)
    .Select(x => new SelectListItem
    {
        Value = x.SourceLocationID.ToString(),
        Text = string.Format("{0} {1} ex {2}", x.CementSources.SampleSource, x.ProductType, x.SamplingLocation)
    });

是,它与渴望/延迟加载相关。您需要删除该属性。相反,您可以使用
ViewBag.SourceLocationID=db.SourceLocations.AsEnumerable().Select(x=>new-SelectListItem{Value=x.SourceLocationID,Text=string.Format(“{0}{1}ex{2}”,x.cencources.SampleSource,x.ProductType,x.sampleLocation})
oar非常感谢您的快速回复。因此,我取消了属性并将此ViewBag添加到控制器中。因此,我修复了ToString()位并运行。我在尝试创建样本时遇到了相同的DataReader问题。'源错误:第66行:ViewBag.SourceLocationID=db.SourceLocations.AsEnumerable()。选择(x=>new SelectListItem{Value=x.SourceLocationID.ToString(),Text=string.Format(“{0}{1}ex{2}”,x.CencoSources.SampleSource,x.ProductType,x.SamplingLocation)});”这不应该发生,因为集合已具体化。但请尝试使用
db.SourceLocations.Include(x=>x.CencoSources).AsEnumerable()…
好吧……这就成功了:-)谢谢堆。你想回答吗?这样我就可以给你投票了?'ViewBag.SourceLocationID=db.SourceLocations.Include(x=>x.cenconSources.AsEnumerable().Select(x=>new-SelectListItem{Value=x.SourceLocationID.ToString(),Text=string.Format(“{0}{1}ex{2}”,x.CementSources.SampleSource,x.ProductType,x.SamplingLocation);“给我30分钟(我以前看过一篇很好的文章解释了这个问题,我想看看是否能找到包含链接的文章)是的,它与渴望/延迟加载相关。您需要删除该属性。相反,您可以使用
ViewBag.SourceLocationID=db.SourceLocations.AsEnumerable().Select(x=>new-SelectListItem{Value=x.SourceLocationID,Text=string.Format(“{0}{1}ex{2}”,x.cencources.sampleSources,x.ProductType,x.SamplingLocation}”)
oar非常感谢您的快速回复。因此,我取消了属性并将此ViewBag添加到控制器中。因此,我修复了ToString()位并运行。我在尝试创建样本时遇到了相同的DataReader问题。'源错误:第66行:ViewBag.SourceLocationID=db.SourceLocations.AsEnumerable()。选择(x=>new SelectListItem{Value=x.SourceLocationID.ToString(),Text=string.Format(“{0}{1}ex{2}”,x.CencoSources.SampleSource,x.ProductType,x.SamplingLocation)});”这不应该发生,因为集合已具体化。但请尝试使用
db.SourceLocations.Include(x=>x.CencoSources).AsEnumerable()…
好吧……这就成功了:-)谢谢堆。你想回答吗?这样我就可以给你投票了?'ViewBag.SourceLocationID=db.SourceLocations.Include(x=>x.cenconSources.AsEnumerable().Select(x=>new-SelectListItem{Value=x.SourceLocationID.ToString(),Text=string.Format(“{0}{1}ex{2}”,x.CementSources.SampleSource,x.ProductType,x.SamplingLocation);“给我30分钟(我以前看过一篇很好的文章解释了这个问题,我想看看是否能找到包含链接的文章)