复合关键问题

本文关键字:问题 复合 | 更新日期: 2023-09-27 18:15:08

我正在制作一个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 class:

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抛出这个错误?

编辑我尝试将YearIDWeek模型中的名称更改为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();

但是现在它告诉我序列不包含匹配的元素,但是有?

复合关键问题

我设法通过将[DatabaseGenerated(DatabaseGeneratedOption.None)]添加到键上方的模型以允许我编辑它们来使其工作。

不应该将外键用作表上复合键的一部分。把你的表改成这样,它就会做你想做的。

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