0

Im trying to figure out this issue; (Ef6)

I have three tables, 'Goal' , 'GoalType', 'GoalBudget'. I have a one to one between Goal and GoalType which works fine, but when I do the same with GoalBudget which does not work - when I 'update-database' I get this error:

Goal_GoalBudget_Source: : Multiplicity is not valid in Role 'Goal_GoalBudget_Source' in relationship 'Goal_GoalBudget'. Because the Dependent Role refers to the key properties, the upper bound of the multiplicity of the Dependent Role must be '1'.

What is the issue - many Thanks

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

The Config is :

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);
graemegets
  • 265
  • 1
  • 2
  • 9

1 Answers1

1

You have an error in your FluentAPI mapping:

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

Your HasForeignKey method is configuring the wrong foreign key column (GoalId), it should be GoalBudgetId instead.

Try:

modelBuilder.Entity<Goal>()
    .HasRequired(o => o.GoalBudget)
    .WithMany(o => o.Goals)
    .HasForeignKey(k => k.GoalBudgetId);
haim770
  • 48,394
  • 7
  • 105
  • 133
  • Wow identical answers. You beat me by a few seconds. :) – Anthony Chu May 21 '14 at 17:21
  • 1
    Please note that you don't really need it anyway, EF is able to determine it automatically using the `NavigationPropertyNameForeignKeyDiscoveryConvention`. See http://msdn.microsoft.com/en-us/library/system.data.entity.modelconfiguration.conventions.navigationpropertynameforeignkeydiscoveryconvention%28v=vs.103%29.aspx. – haim770 May 22 '14 at 09:12