asp.net 4.5 代码优先 类 并在列表视图中显示项目
asp.net 4.5 Code First Classes And Displaying Items In A Listview
我创建了以下 类 然后使用实体优先方法成功创建了一个数据库:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace HR_Test_v0_1.Models
{
public class simStaff
{
[Key]
public int simStaffId { get; set; }
[Required(ErrorMessage ="You must enter a first name")]
public string FirstName { get; set; }
[Required(ErrorMessage ="You must enter a last name")]
public string LastName { get; set; }
public simStaff simHoursRecorded { get; set; }
public simPayRate simPayRate { get; set; }
}
public class simHoursRecorded
{
[Key]
public int simHoursRecordId { get; set; }
[DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)]
public DateTime DayWorked { get; set; }
public int HoursBooked { get; set; }
public bool AuthorisedToPay { get; set; }
public int simStaffId { get; set; }
public ICollection<simStaff> simStaff { get; set; }
}
public class simPayRate
{
[Key]
public int simPayRateId { get; set; }
public double RatePerHour { get; set; }
public DateTime DateAppliesFrom { get; set; }
public int simHoursTypeId { get; set; }
public ICollection<simHoursType> simHoursType { get; set; }
public int simStaffId { get; set; }
public ICollection<simStaff> simStaff { get; set; }
}
public class simHoursType
{
[Key]
public int simHoursTypeId { get; set; }
public string HoursType { get; set; }
public simPayRate simPayRate { get; set; }
}
}
我的上下文class如下:
public class EFContext : DbContext
{
public EFContext() : base("name=HRTestContext")
{
}
public DbSet<simStaff> simStaffs { get; set; }
public DbSet<simHoursRecorded> simHoursRecordeds { get; set; }
public DbSet<simPayRate> simPayRates { get; set; }
public DbSet<simHoursType> simHoursType { get; set; }
}
我的Repository.cs文件中的相关摘录如下:
public IEnumerable<simPayRate> GetPayRate
{
get
{
return context.simPayRates;
}
}
在 ListView 中,我有以下代码。然而,当我输入 Item.simHoursType 时,InsertItemTemplate 在 ItemTemplate 中起作用,我希望看到来自 simHoursType 实体的 HoursType 选项:
<ItemTemplate>
<tr>
<td><%# Item.RatePerHour %></td>
<td><%# Item.DateAppliesFrom.ToShortDateString() %></td>
<td><%# Item.simHoursType %></td>
<td>
<asp:Button CommandName="Edit" Text="Edit" runat="server" />
</td>
</tr>
</ItemTemplate>
<InsertItemTemplate>
<tr>
<td><input id="RatePerHour" runat="server"
value="<%# BindItem.RatePerHour %>" /></td>
<td><input id="DateAppliesFrom" runat="server"
value="<%# BindItem.DateAppliesFrom %>" /></td>
<td>
<asp:DropDownList ID="ddHoursType"
runat="server"
AppendDataBoundItems="true"
SelectMethod="GetHoursType"
DataTextField="HoursType"
DataValueField="simHoursTypeId"
SelectedValue="<%# BindItem.simHoursTypeId %>"
AutoPostBack="false" />
</td>
<td>
<asp:Button CommandName="Insert" runat="server" text="Add" />
<asp:Button CommandName="Cancel" runat="server" Text="Cancel" />
</td>
</tr>
</InsertItemTemplate>
我可以更改 classes 的创建方式以便我可以输入 Item.simHoursType.HoursType,否则我应该如何显示 HoursType?
您不能这样做的原因似乎是因为 Item.simHoursType
实际上是 simHoursType
的 ICollection
。要访问 HoursType
,您必须访问该集合中的单个项目。例如:Item.simHoursType[0].HoursType
.
Code First 约定不正确,它们应该如下:
public class simStaff
{
[Key]
public int simStaffId { get; set; }
[Required(ErrorMessage ="You must enter a first name")]
public string FirstName { get; set; }
[Required(ErrorMessage ="You must enter a last name")]
public string LastName { get; set; }
public ICollection<simHoursRecorded> simHoursRecorded { get; set; }
public ICollection<simPayRate> simPayRate { get; set; }
}
public class simHoursRecorded
{
[Key]
public int simHoursRecordId { get; set; }
[DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)]
public DateTime DayWorked { get; set; }
public int HoursBooked { get; set; }
public bool AuthorisedToPay { get; set; }
public int simStaffId { get; set; }
public simStaff simStaff;
}
public class simPayRate
{
[Key]
public int simPayRateId { get; set; }
public double RatePerHour { get; set; }
public DateTime DateAppliesFrom { get; set; }
public int? simHoursTypeId { get; set; }
public simHoursType simHoursType { get; set; }
public int simStaffId { get; set; }
public simStaff simStaff;
}
public class simHoursType
{
[Key]
public int simHoursTypeId { get; set; }
public string HoursType { get; set; }
public ICollection<simHoursType> simPayRate { get; set; }
}
那么需要在Repository.cs中使用include如下:
public IEnumerable<simPayRate> GetPayRate
{
get
{
return context.simPayRates.Include("simHoursType");
}
}
我创建了以下 类 然后使用实体优先方法成功创建了一个数据库:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace HR_Test_v0_1.Models
{
public class simStaff
{
[Key]
public int simStaffId { get; set; }
[Required(ErrorMessage ="You must enter a first name")]
public string FirstName { get; set; }
[Required(ErrorMessage ="You must enter a last name")]
public string LastName { get; set; }
public simStaff simHoursRecorded { get; set; }
public simPayRate simPayRate { get; set; }
}
public class simHoursRecorded
{
[Key]
public int simHoursRecordId { get; set; }
[DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)]
public DateTime DayWorked { get; set; }
public int HoursBooked { get; set; }
public bool AuthorisedToPay { get; set; }
public int simStaffId { get; set; }
public ICollection<simStaff> simStaff { get; set; }
}
public class simPayRate
{
[Key]
public int simPayRateId { get; set; }
public double RatePerHour { get; set; }
public DateTime DateAppliesFrom { get; set; }
public int simHoursTypeId { get; set; }
public ICollection<simHoursType> simHoursType { get; set; }
public int simStaffId { get; set; }
public ICollection<simStaff> simStaff { get; set; }
}
public class simHoursType
{
[Key]
public int simHoursTypeId { get; set; }
public string HoursType { get; set; }
public simPayRate simPayRate { get; set; }
}
}
我的上下文class如下:
public class EFContext : DbContext
{
public EFContext() : base("name=HRTestContext")
{
}
public DbSet<simStaff> simStaffs { get; set; }
public DbSet<simHoursRecorded> simHoursRecordeds { get; set; }
public DbSet<simPayRate> simPayRates { get; set; }
public DbSet<simHoursType> simHoursType { get; set; }
}
我的Repository.cs文件中的相关摘录如下:
public IEnumerable<simPayRate> GetPayRate
{
get
{
return context.simPayRates;
}
}
在 ListView 中,我有以下代码。然而,当我输入 Item.simHoursType 时,InsertItemTemplate 在 ItemTemplate 中起作用,我希望看到来自 simHoursType 实体的 HoursType 选项:
<ItemTemplate>
<tr>
<td><%# Item.RatePerHour %></td>
<td><%# Item.DateAppliesFrom.ToShortDateString() %></td>
<td><%# Item.simHoursType %></td>
<td>
<asp:Button CommandName="Edit" Text="Edit" runat="server" />
</td>
</tr>
</ItemTemplate>
<InsertItemTemplate>
<tr>
<td><input id="RatePerHour" runat="server"
value="<%# BindItem.RatePerHour %>" /></td>
<td><input id="DateAppliesFrom" runat="server"
value="<%# BindItem.DateAppliesFrom %>" /></td>
<td>
<asp:DropDownList ID="ddHoursType"
runat="server"
AppendDataBoundItems="true"
SelectMethod="GetHoursType"
DataTextField="HoursType"
DataValueField="simHoursTypeId"
SelectedValue="<%# BindItem.simHoursTypeId %>"
AutoPostBack="false" />
</td>
<td>
<asp:Button CommandName="Insert" runat="server" text="Add" />
<asp:Button CommandName="Cancel" runat="server" Text="Cancel" />
</td>
</tr>
</InsertItemTemplate>
我可以更改 classes 的创建方式以便我可以输入 Item.simHoursType.HoursType,否则我应该如何显示 HoursType?
您不能这样做的原因似乎是因为 Item.simHoursType
实际上是 simHoursType
的 ICollection
。要访问 HoursType
,您必须访问该集合中的单个项目。例如:Item.simHoursType[0].HoursType
.
Code First 约定不正确,它们应该如下:
public class simStaff
{
[Key]
public int simStaffId { get; set; }
[Required(ErrorMessage ="You must enter a first name")]
public string FirstName { get; set; }
[Required(ErrorMessage ="You must enter a last name")]
public string LastName { get; set; }
public ICollection<simHoursRecorded> simHoursRecorded { get; set; }
public ICollection<simPayRate> simPayRate { get; set; }
}
public class simHoursRecorded
{
[Key]
public int simHoursRecordId { get; set; }
[DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)]
public DateTime DayWorked { get; set; }
public int HoursBooked { get; set; }
public bool AuthorisedToPay { get; set; }
public int simStaffId { get; set; }
public simStaff simStaff;
}
public class simPayRate
{
[Key]
public int simPayRateId { get; set; }
public double RatePerHour { get; set; }
public DateTime DateAppliesFrom { get; set; }
public int? simHoursTypeId { get; set; }
public simHoursType simHoursType { get; set; }
public int simStaffId { get; set; }
public simStaff simStaff;
}
public class simHoursType
{
[Key]
public int simHoursTypeId { get; set; }
public string HoursType { get; set; }
public ICollection<simHoursType> simPayRate { get; set; }
}
那么需要在Repository.cs中使用include如下:
public IEnumerable<simPayRate> GetPayRate
{
get
{
return context.simPayRates.Include("simHoursType");
}
}