实体框架存储库 [英] Entity Framework Repository
问题描述
如下面的代码所示.
命名空间DAL
{
[MetadataType(typeof(DepartmentMetaData))
公共部分课程部门
{
}
公共类DepartmentMetaData
{
[DataType(DataType.Currency)]
[范围( 0 , 1000000 ,ErrorMessage = " 预算必须少于$ 1,000,000.00")]
公共 十进制预算{get; 设置; }
[DisplayFormat(DataFormatString = " ,ApplyFormatInEditMode = true)]
公共 DateTime StartDate {get; 设置; }
}
}
在表示层中,我绑定到了gridview,如下所示:
< asp:ObjectDataSource ID =" runat =" 服务器" TypeName DAL.SchoolRepository"
span> DataObjectTypeName =" DAL.部门" SelectMethod GetDepartments" < /asp:ObjectDataSource >
< asp:GridView ID =" runat 服务器" DataSourceID obj" AutoGenerateColumns 错误" < 列 >
< asp:BoundField DataField =" HeaderText DepartmentID " SortExpression 部门ID" 可见 false" < /asp:BoundField >
< asp:BoundField 数据字段 =" HeaderText 名称" SortExpression 名称" < /asp:BoundField >
< asp:BoundField DataField =" HeaderText 预算" SortExpression 预算" < /asp:BoundField >
< asp:BoundField DataField =" HeaderText StartDate " SortExpression 开始日期" < /asp:BoundField >
< asp:TemplateField HeaderText =" < ItemTemplate >
< asp:Label ID =" runat 服务器" 文本 <%#Eval(" )%> ' / >
< asp:Label ID =" runat 服务器" 文本 <%#Eval(" )%> ' / >
< /ItemTemplate >
< /asp:TemplateField >
< /列 >
< /asp:GridView >
在aspx.cs中
我这样写了page_init事件.
受保护的 无效 Page_Init(对象发件人,EventArgs e)
{
gv.EnableDynamicData( typeof (部门));
}
}
在预算中,它应显示美元货币符号和开始日期格式,如DAL部门中所述.cs
请帮我谢谢.
1,000,000.00")] 公共 十进制预算{get; 设置; } [DisplayFormat(DataFormatString = " ,ApplyFormatInEditMode = true)] 公共 DateTime StartDate {get; 设置; } } }
在表示层中,我绑定到了gridview,如下所示:
< asp:ObjectDataSource ID =" runat =" 服务器" TypeName DAL.SchoolRepository" span> DataObjectTypeName =" DAL.部门" SelectMethod GetDepartments" < /asp:ObjectDataSource > < asp:GridView ID =" runat 服务器" DataSourceID obj" AutoGenerateColumns 错误" < 列 > < asp:BoundField DataField =" HeaderText DepartmentID " SortExpression 部门ID" 可见 false" < /asp:BoundField > < asp:BoundField 数据字段 =" HeaderText 名称" SortExpression 名称" < /asp:BoundField > < asp:BoundField DataField =" HeaderText 预算" SortExpression 预算" < /asp:BoundField > < asp:BoundField DataField =" HeaderText StartDate " SortExpression 开始日期" < /asp:BoundField > < asp:TemplateField HeaderText =" < ItemTemplate > < asp:Label ID =" runat 服务器" 文本 <%#Eval(" )%> ' / > < asp:Label ID =" runat 服务器" 文本 <%#Eval(" )%> ' / > < /ItemTemplate > < /asp:TemplateField > < /列 > < /asp:GridView >
在aspx.cs中
我这样写了page_init事件.
受保护的 无效 Page_Init(对象发件人,EventArgs e) { gv.EnableDynamicData( typeof (部门)); } }
在预算中,它应显示美元货币符号和开始日期格式,如DAL部门中所述.cs
请帮帮我,谢谢.
已通过这种方式定义了预算和开始日期列-
< asp:boundfield 数据字段 =" 标题文本 =" 预算" sortexpression 预算" dataformatstring =" > < /asp:boundfield > < asp:boundfield 数据字段 =" 标题文字 StartDate " sortexpression StartDate" 数据格式字符串 {0:d}" > < /asp:boundfield >
恕我直言,这比在DAL上显示格式要好.
i have created entity data model in DAL. In DAL i have created class Department
as shown in below code.
namespace DAL
{
[MetadataType(typeof(DepartmentMetaData))]
public partial class Department
{
}
public class DepartmentMetaData
{
[DataType(DataType.Currency)]
[Range(0, 1000000, ErrorMessage = "Budget must be less than $1,000,000.00")]
public Decimal Budget { get; set; }
[DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)]
public DateTime StartDate { get; set; }
}
}
in presentation layer i binded to gridview like this shown below:
<asp:ObjectDataSource ID="obj" runat="server" TypeName="DAL.SchoolRepository"
DataObjectTypeName="DAL.Department" SelectMethod="GetDepartments">
</asp:ObjectDataSource>
<asp:GridView ID="gv" runat="server" DataSourceID="obj" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="DepartmentID" HeaderText="DepartmentID" SortExpression="DepartmentID" Visible="false">
</asp:BoundField>
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name"></asp:BoundField>
<asp:BoundField DataField="Budget" HeaderText="Budget" SortExpression="Budget"></asp:BoundField>
<asp:BoundField DataField="StartDate" HeaderText="StartDate" SortExpression="StartDate">
</asp:BoundField>
<asp:TemplateField HeaderText="Administrator">
<ItemTemplate>
<asp:Label ID="lblLastName" runat="server" Text='<%# Eval("Person.LastName") %>' />
<asp:Label ID="lblFirstName" runat="server" Text='<%# Eval("Person.FirstName") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
in that aspx.cs
i wrote page_init event like this.
protected void Page_Init(object sender, EventArgs e)
{
gv.EnableDynamicData(typeof(Department));
}
}
here in budget it should show dollar currency symbol and start date format like wrote in DAL department.cs
please help me thank you.
1,000,000.00")] public Decimal Budget { get; set; } [DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)] public DateTime StartDate { get; set; } } }
in presentation layer i binded to gridview like this shown below:
<asp:ObjectDataSource ID="obj" runat="server" TypeName="DAL.SchoolRepository" DataObjectTypeName="DAL.Department" SelectMethod="GetDepartments"> </asp:ObjectDataSource> <asp:GridView ID="gv" runat="server" DataSourceID="obj" AutoGenerateColumns="False"> <Columns> <asp:BoundField DataField="DepartmentID" HeaderText="DepartmentID" SortExpression="DepartmentID" Visible="false"> </asp:BoundField> <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name"></asp:BoundField> <asp:BoundField DataField="Budget" HeaderText="Budget" SortExpression="Budget"></asp:BoundField> <asp:BoundField DataField="StartDate" HeaderText="StartDate" SortExpression="StartDate"> </asp:BoundField> <asp:TemplateField HeaderText="Administrator"> <ItemTemplate> <asp:Label ID="lblLastName" runat="server" Text='<%# Eval("Person.LastName") %>' /> <asp:Label ID="lblFirstName" runat="server" Text='<%# Eval("Person.FirstName") %>' /> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView>
in that aspx.cs
i wrote page_init event like this.
protected void Page_Init(object sender, EventArgs e) { gv.EnableDynamicData(typeof(Department)); } }
here in budget it should show dollar currency symbol and start date format like wrote in DAL department.cs
please help me thank you.
Have the budget and start date columns defined this way -
<asp:boundfield datafield="Budget" headertext="Budget" sortexpression="Budget" dataformatstring="{0:C}" ></asp:boundfield> <asp:boundfield datafield="StartDate" headertext="StartDate" sortexpression="StartDate" dataformatstring="{0:d}" > </asp:boundfield>
IMHO, this is better than having the display formatting on the DAL.
这篇关于实体框架存储库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!