【发布时间】:2019-06-23 08:18:45
【问题描述】:
我正在创建一个 Visual Studios MVC5 Web 应用程序,并尝试将 AspNetUser 表中的 id 作为外键引用。
我正在创建一个新表:
public class Patient
{
public int PatientId { get; set; }
public string HCN { get; set; }
public string GP { get; set; }
public string MedHis { get; set; }
public string Medication { get; set; }
public string CurrentPrescription { get; set; }
public string PresentRX { get; set; }
[ForeignKey("UserId")]
public virtual ApplicationUser UserId { get; set; }
}
我的Patient 配置类:
public class PatientConfig : EntityTypeConfiguration<Patient>
{
public PatientConfig()
{
ToTable("Patient");
Property(x => x.PatientId).HasColumnName("IntId");
HasKey(x => x.PatientId);
Property(x => x.HCN).HasColumnName("strHCN");
Property(x => x.GP).HasColumnName("strGP");
Property(x => x.MedHis).HasColumnName("strMedHis");
Property(x => x.Medication).HasColumnName("strMedication");
Property(x => x.CurrentPrescription).HasColumnName("strCurrentPrescription");
Property(x => x.PresentRX).HasColumnName("strPresentRX");
Property(x => x.UserId).HasColumnName("intUserId");
}
}
首先我得到了错误
必须是不可为空的值类型才能将其用作参数
在UserId 属性下面,所以我将其删除了
Property(x=> x.UserId).HasColumnName("intUserId");
当我尝试添加迁移时,出现以下错误:
类型“ModelTest.Models.Patient”的属性“UserId”上的 ForeignKeyAttribute 无效。在依赖类型“ModelTest.Models.Patient”上找不到外键名称“UserId”。 Name 值应该是一个逗号分隔的外键属性名称列表。
我不明白为什么找不到UserId,任何建议将不胜感激。
谢谢
【问题讨论】:
标签: c# asp.net-mvc entity-framework visual-studio-2013