【问题标题】:Fluent API issue - Multiplicity is not valid in RoleFluent API 问题 - 多重性在角色中无效
【发布时间】:2014-05-21 17:09:16
【问题描述】:

我正在尝试解决这个问题; (Ef6)

我有三个表,“Goal”、“GoalType”、“GoalBudget”。我在 Goal 和 GoalType 之间有一对一的关系,它工作正常,但是当我对 GoalBudget 做同样的事情时,它不起作用 - 当我“更新数据库”时,我收到这个错误:

Goal_GoalBudget_Source::多重性在关系“Goal_GoalBudget”中的角色“Goal_GoalBudget_Source”中无效。因为从属角色是指关键属性,所以从属角色的多重性的上限必须是'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);

【问题讨论】:

    标签: c# .net entity-framework fluent


    【解决方案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);
    

    【讨论】:

    • 哇,答案完全相同。你赢了我几秒钟。 :)
    • 请注意,您实际上并不需要它,EF 能够使用NavigationPropertyNameForeignKeyDiscoveryConvention 自动确定它。见msdn.microsoft.com/en-us/library/…
    猜你喜欢
    • 1970-01-01
    • 2014-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-28
    • 1970-01-01
    相关资源
    最近更新 更多