【发布时间】:2016-04-20 18:55:00
【问题描述】:
我想在 EF 中与 MVC 建立一对一的关系,但有点困惑
这是我的模型...
public class Event
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public Guid Id { get; set; }
public Guid CategoryId { get; set; }
public string EventTitle { get; set; }
public string ImgUrl { get; set; }
public string Venue { get; set; }
public int SeatsAvailable { get; set; }
public string Description { get; set; }
public DateTime EventDate { get; set; }
public DateTime EventAddedDate { get; set; }
[Required]
[ForeignKey("CategoryId")]
public virtual Category Category { get; set; }
}
和
public class Category
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public Guid Id { get; set; }
public string CategoryName { get; set; }
public virtual Event Event { get; set; }
}
我想将类别 ID 存储在 Event 中,所以我像这样在 fluentApi 中创建了关系...
modelBuilder.Entity<Event>()
.HasRequired(a => a.Category)
.WithOptional(b => b.Event);
但更新数据库时出现此错误...
System.Data.Entity.Edm.EdmAssociationEnd: : Multiplicity is not valid in Role 'Event_Category_Source' in relationship 'Event_Category'. Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be '*'.
【问题讨论】:
标签: c# entity-framework model-view-controller ef-code-first ef-fluent-api