我正在创建一个MVC应用程序,它接受每日用户输入并将其放入表中。它分为几天,几周和几个月,我正在尝试使用复合键,但是我收到了错误。
以下是我的模特:
Year
型号:
public class Year
{
[ForeignKey("YearID")]
public int YearID { get; set; }
public virtual ICollection<Week> Weeks { get; set; }
}
Week
型号:
public class Week
{
[Key, Column(Order = 1)]
public int WeekID { get; set; }
[Key, Column(Order = 2)]
public int YearID { get; set; }
public virtual Year year { get; set; }
public virtual ICollection<Day> Days { get; set; }
}
最后,Day
模型:
public class Day
{
[Key, Column(Order = 1)]
public int DayID { get; set; }
[Key, Column(Order = 2)]
public int WeekID { get; set; }
[Key, Column(Order = 3)]
public int YearID { get; set; }
public float Reading1 { get; set; }
public float Reading2 { get; set; }
public virtual Week Weeks { get; set; }
}
UtilityInitializer
上课:
namespace Utility3.DAL
{
public class UtilityInitializer : System.Data.Entity.DropCreateDatabaseAlways<UtilityContext>
{
protected override void Seed(UtilityContext context)
{
var years = new List<Year>
{
new Year{YearID = 2014},
new Year{YearID = 2015}
};
years.ForEach(s => context.Years.Add(s));
context.SaveChanges();
var weeks = new List<Week>
{
new Week{WeekID = 1, YearID = 2014},
new Week{WeekID = 2, YearID = 2014},
new Week{WeekID = 1, YearID = 2015},
new Week{WeekID = 2, YearID = 2015}
};
weeks.ForEach(s => context.Weeks.Add(s));
context.SaveChanges();
var days = new List<Day>
{
new Day{DayID = 1, WeekID = 1, YearID = 2014},
new Day{DayID = 2, WeekID = 1, YearID = 2014},
new Day{DayID = 1, WeekID = 2, YearID = 2014},
new Day{DayID = 2, WeekID = 2, YearID = 2014},
new Day{DayID = 1, WeekID = 1, YearID = 2015},
new Day{DayID = 2, WeekID = 1, YearID = 2015},
new Day{DayID = 1, WeekID = 2, YearID = 2015},
new Day{DayID = 2, WeekID = 2, YearID = 2015}
};
days.ForEach(s => context.Days.Add(s));
context.SaveChanges();
}
}
}
当我尝试制作控制器时,我收到此错误:
Unable to retrieve metadata from "Utility3.Models.Year". The property "YearID" cannot be configured as a navigation property. The property must be a valid entity type and the property should have a non-abstract getter and setter. For collection properties the type must implement ICollection<T> where T is a valid entity type.
我尝试YearID
作为外键来解决问题。没有外键我得到了一个不同的错误。
为什么YearID会抛出该错误?
修改
我尝试将Week
YearID
模型中的名称更改为YearNo
,并在初始化程序中更改了此段代码:
var weeks = new List<Week>
{
new Week{YearNo = years.Single(i => i.YearID == 2014).YearID, WeekID = 1},
new Week{YearNo = years.Single(i => i.YearID == 2014).YearID, WeekID = 2},
new Week{YearNo = years.Single(i => i.YearID == 2015).YearID, WeekID = 1},
new Week{YearNo = years.Single(i => i.YearID == 2015).YearID, WeekID = 2}
};
weeks.ForEach(s => context.Weeks.Add(s));
context.SaveChanges();
但是现在告诉我序列中没有匹配的元素但是有?
答案 0 :(得分:1)
我设法通过将[DatabaseGenerated(DatabaseGeneratedOption.None)]
添加到键上方的模型来让它工作,以便我可以编辑它们。它现在以任何方式工作。
答案 1 :(得分:0)
不应将外键用作表上组合键的一部分。改变你的表格就像这样工作,它会做你想做的事。
public class Year
{
[Key]
public int YearID { get; set; }
public virtual ICollection<Week> Weeks { get; set; }
}
public class Week
{
[Key]
public int WeekID { get; set; }
public virtual Year year { get; set; }
public virtual ICollection<Day> Days { get; set; }
}
public class Day
{
[Key]
public int DayID { get; set; }
public float Reading1 { get; set; }
public float Reading2 { get; set; }
public virtual Week Weeks { get; set; }
}