C# 实体框架-外键组件&x2026;不是类型的声明属性
我有下面的型号C# 实体框架-外键组件&x2026;不是类型的声明属性,c#,.net,entity-framework,ef-code-first,code-first,C#,.net,Entity Framework,Ef Code First,Code First,我有下面的型号 public class FilanthropyEvent : EntityBase, IDeleteable { public int Id { get; set; } public string Name { get; set; } public DateTime EventDate { get; set; } public string Description { get; set; } public decimal Targe
public class FilanthropyEvent : EntityBase, IDeleteable
{
public int Id { get; set; }
public string Name { get; set; }
public DateTime EventDate { get; set; }
public string Description { get; set; }
public decimal Target { get; set; }
public decimal EntryFee { get; set; }
public bool Deleted { get; set; }
public ICollection<EventAttendee> EventAttendees { get; set; }
}
public class Attendee : EntityBase, IDeleteable
{
public int Id { get; set; }
public string Email { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public bool MailingList { get; set; }
public bool Deleted { get; set; }
public ICollection<EventAttendee> EventAttendees { get; set; }
}
这些是每个FilanthropyEvent和Attendee的配置
public class FilanthropyEventConfiguration : EntityTypeConfiguration<FilanthropyEvent>
{
public FilanthropyEventConfiguration()
{
HasKey(x => x.Id);
Property(x => x.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
HasMany(x => x.EventAttendees).WithRequired(x => x.FilanthropyEvent).HasForeignKey(x => x.FilanthropyEvent);
}
}
public AttendeeConfiguration()
{
HasKey(x => x.Id);
Property(x => x.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
HasMany(x => x.EventAttendees).WithRequired(x => x.Attendee).HasForeignKey(x => x.AttendeeId);
}
public class EventAttendeesConfiguration : EntityTypeConfiguration<EventAttendee>
{
public EventAttendeesConfiguration()
{
HasKey(x => new {x.FilanthropyEventId, x.AttendeeId});
}
}
公共类FilanthropyEventConfiguration:EntityTypeConfiguration
{
公共文件配置()
{
HasKey(x=>x.Id);
属性(x=>x.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
HasMany(x=>x.EventAttenders)。WithRequired(x=>x.FilanthropyEvent)。HasForeignKey(x=>x.FilanthropyEvent);
}
}
公共配置()
{
HasKey(x=>x.Id);
属性(x=>x.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
HasMany(x=>x.EventAttendes)。WithRequired(x=>x.Attendee)。HasForeignKey(x=>x.AttendeeId);
}
公共类事件与会者配置:EntityTypeConfiguration
{
公共事件与会者配置()
{
HasKey(x=>new{x.filantropyeventid,x.attendeid});
}
}
当我尝试通过package manager控制台中的update database
命令初始化数据库时,出现以下错误
System.InvalidOperationException:外键组件“FilanthropyEvent”不是类型“EventAttendee”上声明的属性。验证它是否未从模型中明确排除,并且它是一个有效的基元属性
我意识到我可能在EventAttendeesConfiguration
类中缺少映射,但是建立这种关系模型的正确映射是什么呢?此代码
HasMany(x => x.EventAttendees)
.WithRequired(x => x.FilanthropyEvent)
.HasForeignKey(x => x.FilanthropyEvent);
应该是
HasMany(x => x.EventAttendees)
.WithRequired(x => x.FilanthropyEvent)
.HasForeignKey(x => x.FilanthropyEventId);
Blimey(…)
--应为异常类型。我也做了同样的事Doh(…)
:(我想这是一个常见的愚蠢错误,也是这样做的,我责备复制和粘贴:)
HasMany(x => x.EventAttendees)
.WithRequired(x => x.FilanthropyEvent)
.HasForeignKey(x => x.FilanthropyEventId);