Fluent API问题 - Multiplicity在Role中无效

时间:2014-05-21 17:09:17

标签: c# .net entity-framework fluent

我试图找出这个问题; (EF6)

我有三张桌子,'目标' ,' GoalType',' GoalBudget'。我在Goal和GoalType之间有一对一的工作正常,但是当我对GoalBudget执行相同操作时,这不起作用 - 当我更新 - 数据库'我收到这个错误:

Goal_GoalBudget_Source ::多重性在角色' Goal_GoalBudget_Source'中无效。在关系' Goal_GoalBudget'。由于“从属角色”是指关键属性,因此“从属角色”的多重性的上限必须为“' 1”。

问题是什么 - 非常感谢

public class Goal
{
    public int GoalId {get;set;}
    public string Name { get; set; }
    public string Description { get; set; }
    public decimal TargetAmount { get; set; }

    public int GoalBudgetId { get; set; }
    public int GoalTypeId { get; set; }
    // Navigaiton Properties
    public virtual GoalType GoalType { get; set; }
    public virtual GoalBudget GoalBudget{ get; set; }
}

public class GoalBudget
{
    public GoalBudget()
    {
        Goals = new List<Goal>();
    }
    public int GoalBudgetId { get; set; }
    public DateTime Created { get; set; }
    // Navigation Property
    public ICollection<Goal> Goals { get; set; }
}

public class GoalType
{
    public GoalType()
    {
        Goals = new List<Goal>();
    }
    public int GoalTypeId {get;set;}
    public string Name { get; set; }
    // Navigation Property
    public ICollection<Goal> Goals { get; set; }
}

配置是:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        // GOAL 
        modelBuilder.Entity<Goal>().Property(n => n.GoalId).IsRequired();
        modelBuilder.Entity<Goal>().Property(n => n.GoalId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
        modelBuilder.Entity<Goal>().Property(n => n.Name).IsRequired();
        modelBuilder.Entity<Goal>().Property(n => n.Name).HasMaxLength(50);
        modelBuilder.Entity<Goal>().Property(n => n.TargetAmount).IsRequired();

        //-
        modelBuilder.Entity<Goal>().HasKey(k => k.GoalId);
        modelBuilder.Entity<Goal>().HasRequired(o => o.GoalType).WithMany(o => o.Goals).HasForeignKey(k => k.GoalTypeId) ;
        modelBuilder.Entity<Goal>().HasRequired(o => o.GoalBudget).WithMany(o => o.Goals).HasForeignKey(k => k.GoalId);

        // GOAL TYPE
        modelBuilder.Entity<GoalType>().HasKey(k => k.GoalTypeId);
        // GOAL BUDGET
        modelBuilder.Entity<GoalBudget>().Property(n => n.GoalBudgetId).IsRequired();
        modelBuilder.Entity<GoalBudget>().Property(n => n.GoalBudgetId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
        modelBuilder.Entity<GoalBudget>().Property(n => n.Created).IsRequired();
        modelBuilder.Entity<GoalBudget>().HasKey(k => k.GoalBudgetId);

1 个答案:

答案 0 :(得分:1)

您的FluentAPI映射中有错误:

modelBuilder.Entity<Goal>()
    .HasRequired(o => o.GoalBudget)
    .WithMany(o => o.Goals)
    .HasForeignKey(k => k.GoalId);

您的HasForeignKey方法正在配置错误的外键列(GoalId),而应该是GoalBudgetId

尝试:

modelBuilder.Entity<Goal>()
    .HasRequired(o => o.GoalBudget)
    .WithMany(o => o.Goals)
    .HasForeignKey(k => k.GoalBudgetId);