Organising Weeks and Days

为君一笑 提交于 2019-12-11 09:07:41

问题


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


来源:https://stackoverflow.com/questions/24930378/organising-weeks-and-days

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!