我正在与C#
学习MVC
和ASP.NET
。我正在制作一个应用程序,它会将每日数据分成几个星期。
我的问题是:我应该如何制作实体?我希望它有一个WeekNo
和一个DayNo
,您可以使用它们来显示,编辑和创建数据和表格。例如,WeekNo = 1
和DayNo = 1
将是第1周的星期一。WeekNo = 1
和DayNo = 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; }
}
外键是白天还是周,还是我需要呢?
答案 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);
),或者至少是某种帮助。