3

我是 ASP.NET 的新手,无法为我得到的异常找到合法的响应。我有一个像这样设置的基本联系人实体。

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;

namespace admin.Models
{
    public class ContactBase
{
    [System.ComponentModel.DataAnnotations.Key]
    public Guid ContactId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public DateTime? BirthDate { get; set; }
    [ForeignKey ("ContactId")]
    public virtual ICollection<ContactExtensionBase> ContactExtensionBases { get; set; }
    [ForeignKey("CustomerAddressId")]
    public virtual ICollection<CustomerAddressBase> CustomerAddressBases { get; set; }
    [ForeignKey("Datatel_academictermId")]
    public virtual ICollection<Datatel_academictermExtensionBase> Datatel_academictermExtensionBases { get; set; }
    [ForeignKey("Datatel_decisionplanId")]
    public virtual ICollection<Datatel_decisionplanExtensionBase> Datatel_decisionplanExtensionBases { get; set; } 
}

}

然后有一个具有更多联系信息的实体,我与之建立了虚拟连接

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;

namespace admin.Models
{
    public class ContactExtensionBase
{
    [System.ComponentModel.DataAnnotations.Key]
    public Guid ContactId { get; set; }
    public string Datatel_erpid { get; set; }
    public string Datatel_ssn { get; set; }
    public Guid datatel_anticipatedentrytermid { get; set; }
    public Guid datatel_decisionplanid { get; set; }
    public virtual ContactBase ContactBases { get; set; }
    }
}

这是我用来显示这两个实体的信息的视图

@model admin.Models.ContactBase

@{
    ViewBag.Title = "Details";
}

<h2>Details</h2>

<fieldset>
<legend>ContactBase</legend>

<div class="display-label">
     @Html.DisplayNameFor(model => model.FirstName)
</div>
<div class="display-field">
    @Html.DisplayFor(model => model.FirstName)
</div>

<div class="display-label">
     @Html.DisplayNameFor(model => model.LastName)
</div>
<div class="display-field">
    @Html.DisplayFor(model => model.LastName)
</div>

<div class="display-label">
     @Html.DisplayNameFor(model => model.BirthDate)
</div>
<div class="display-field">
    @Html.DisplayFor(model => model.BirthDate)
</div>
<div class="display-label">ERPID</div>
<div class="display-field">
   @Html.DisplayFor(model => model.ContactExtensionBases)
</div>
    </fieldset>
<p>
@Html.ActionLink("Edit", "Edit", new { id=Model.ContactId }) |
@Html.ActionLink("Back to List", "Index")
</p>

这是我的上下文

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using admin.Models;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;

namespace admin.Models
{
public class RecruiterTest_MSCRMContext : DbContext
{
    public DbSet<ContactBase> ContactBase { get; set; }
    public DbSet<ContactExtensionBase> ContactExtensionBase { get; set; }
    public DbSet<CustomerAddressBase> CustomerAddressBase { get; set; }
    public DbSet<Datatel_academictermExtensionBase> Datatel_academictermExtensionBase { get; set; }
    public DbSet<Datatel_decisionplanExtensionBase> Datatel_decisionplanExtensionBase { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();


        }

    }
}

每当我尝试查看详细信息页面时,都会出现以下异常:

无法在实体类型 System.Data.Entity.DynamicProxies.ContactBase_97C960FB8ACFC3256638071319F26587D2DD8109C9B58E668A100CFAE56F6E4B 上设置字段/属性 ContactExtensionBases。有关详细信息,请参阅内部异常。

内部异常空

4

1 回答 1

0

ContactExtensionBase 的 ContactBases 可能需要是一个 ICollection:

public class ContactExtensionBase
{
  [System.ComponentModel.DataAnnotations.Key]
  public Guid ContactId { get; set; }
  public string Datatel_erpid { get; set; }
  public string Datatel_ssn { get; set; }
  public Guid datatel_anticipatedentrytermid { get; set; }
  public Guid datatel_decisionplanid { get; set; }
  public virtual ICollection<ContactBase> ContactBases { get; set; }
}
于 2013-06-13T16:50:32.093 回答