gpt4 book ai didi

asp.net - 如何修复 "unable to set field/property...on entity type"

转载 作者:行者123 更新时间:2023-12-05 01:31:33 24 4
gpt4 key购买 nike

我是 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; }
}
}

这是我用来显示来自这两个实体的信息的 View

@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 为空

最佳答案

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; }
}

关于asp.net - 如何修复 "unable to set field/property...on entity type",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16322615/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com