【发布时间】: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