周和日实体

时间:2014-07-10 07:58:49

标签: c# asp.net-mvc entity-framework

我正在与C#学习MVCASP.NET。我正在制作一个应用程序,它会将每日数据分成几个星期。

我的问题是:我应该如何制作实体?我希望它有一个WeekNo和一个DayNo,您可以使用它们来显示,编辑和创建数据和表格。例如,WeekNo = 1DayNo = 1将是第1周的星期一。WeekNo = 1DayNo = 2将是星期二,依此类推。

我是新手,并不擅长信息管理。我需要外键吗?

我在想:

public class Day
{
    public int WeekNo { get; set; }
    public int DayID { get; set; }
    public int DayNo { get; set; }
    public string DayofWeek { get; set; }

    //then other declarations, not important

    [ForeignKey("DayNo")]
}

public class Week
{
    public int ID { get; set; }
    public int WeekNo { get; set; }
    public int DayofWeek { get; set; }
}

外键是白天还是周,还是我需要呢?

1 个答案:

答案 0 :(得分:0)

在说出Object时,假设您的意思是entities s

以下是如何构建Object以便每天使用

public class myDailyObject
{
    public myDailyObject()
    { }

    public int ObjectID { get; set; }
    public int WeekNo { get; set; }
    public int DayNo { get; set; }
    public string DayofWeek { get; set; }

    //Other variables can go here.
}

否则,您可以使用以下结构每周

public class myWeeklyObject
{
    public myWeeklyObject()
    {
        DataByDay = new Dictionary<int, myDataObject>();
    }

    public int ObjectID { get; set; }
    public int WeekNo { get; set; }
    public Dictionary<int, myDataObject> DataByDay { get; set; }
}

public class myDataObject
{
    //Other variables can go here.
}

在此处,您可以使用名为Dictionary<int, myDataObject>的{​​{1}}包含一周中每一天DataByDay myDataObject的实例。


使用示例

The Weekly对象:

Object

如果本周已存在此对象,您也可以从某处获取此对象。

现在,日常对象:

//Declare the weekly object:
myWeeklyObject thisWeek = new myWeeklyObject();
//Populate variables of thisWeek here:
thisWeek.ObjectID = thisWeek.WeekNo = CultureInfo.InvariantCulture.Calendar.GetWeekOfYear(DateTime.Now, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
//The line above gets the current weekNo for the year.

我希望这可以回答你的问题(以及未来的问题,例如如何获得//Get the day of the week as an int: int dayOfWeek = (int)DateTime.Now.DayOfWeek + 1; //DayOfWeek is 0 based //Daclare object for a specific day: myDataObject newObject = new myDataObject(); //Populate variables of newObject here. //Then add it with the int dayOfWeek as the Key to the DataByDay Dictionary. thisWeek.DataByDay.Add(dayOfWeek, newObject); ),或者至少是某种帮助。

祝你好运。