Organising Weeks and Days

2019-08-06 08:27发布

问题:

I am making an MVC application that requires daily readings which are organised into weeks, which are in turn organised into years. I have a week entity, and a day entity, which are tables of weeks and days respectively. Is there any way I can link the days to a WeekNo but not have WeekNo or DayNo as the primary keys?

For example, there'd be 5 working days in week 1:

WeekNo = 1, DayNo = 1
WeekNo = 1, DayNo = 2
WeekNo = 1, DayNo = 3
WeekNo = 1, DayNo = 4
WeekNo = 1, DayNo = 5

Then when Week 2 comes along:

WeekNo = 1, DayNo = 1 

But then there is a duplicate of Day 1.

I am having a similar problem with the weeks in a year, that when the next year comes along and it's back to Week 1, there's a duplicate in weeks. But if I can get past this, maybe I would be able figure that out too.

How would I go about referencing these to each other?

Day model:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Utility.Models
{
    public class Day
    {
        public int DayID { get; set; }
        public int WeekNo { get; set; }
        public int DayNo { get; set; }
        public string DayofWeek { get; set; }
        public float Reading1 { get; set; }
        public float Reading2 { get; set; }
        //etc

        public virtual Week Week { get; set; }

    }
}

Week model:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations.Schema;

namespace Utility.Models
{
    public class Week
    {
        public int WeekID { get; set; }
        public int WeekNo { get; set; }
        public int YearID { get; set; }

        public virtual Year Year { get; set; }
        public virtual ICollection<Day> Days { get; set; }

    }
}

回答1:

How about many to many relationship?

public class Day
{
    public int DayID { get; set; }
    public int DayNo { get; set; }
    public virtual ICollection<WeekDay> WeekDays { get; set; }
}
public class Week
{
    public int WeekID { get; set; }
    public int WeekNo { get; set; }
    public virtual ICollection<WeekDay> WeekDays { get; set; }
}
public class WeekDay
{
    public int WeekDayID { get; set; }
    public int DayID { get; set; }
    public virtual Day Day { get; set; }
    public int WeekID { get; set; }
    public virtual Week Week { get; set; }
}