我是ASP.NET的新手,无法找到我得到的Exception的合法回复。我有一个像这样的基本联系实体。
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。有关详细信息,请参阅InnerException。
InnerException Null
答案 0 :(得分: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; }
}