整个 .net CRUD 是一个很大的领域,有很多风格和工作方式。虽然我不知道你在这方面的确切位置,但以下是我的帮助。以我的经验,EF 可以很好地处理人际关系,尽管整个 EF 学习过程有点陡峭,我已经避开了它。我通常使用带有扩展的 Dapper 并伪手动地做一些事情。我没有使用过 SimpleCrud 扩展。由于您继承了数据库,因此希望它设置良好,并且对 Distribution, Column EntryID 有一个 FK 约束。
在 Dapper 中,你可以设置你的类,比如:
using Dapper.Contrib.Extensions;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
namespace Jacrys
{
[Table("dbo.address_book")]
public partial class AddressBook
{
[Dapper.Contrib.Extensions.Key]
public int EntryID { get; set; }
public string Last_Name { get; set; }
public string First_Name { get; set; }
public string Title { get; set; }
public string Office_Num { get; set; }
public string Cell_Num { get; set; }
public string Home_Num { get; set; }
public string Email_Address { get; set; }
public bool Special_Info { get; set; }
public bool hr24_Emails { get; set; }
public bool hr48_Emails { get; set; }
public bool RM_Emails { get; set; }
public bool Prestige_Emails { get; set; }
public bool GEB_Emails { get; set; }
public bool LAW_Emails { get; set; }
//use this only if you need all of the distributions to be
//part of your main AddressBook class
public IEnumerable<Distribution> Distributions { get; set; }
public static AddressBook GetById(short id)
{
using (IDbConnection cn = new SqlConnection("getConnString"))
{
cn.Open();
return cn.Get<AddressBook>(id);
}
}
public static IEnumerable<AddressBook> GetAll()
{
using (IDbConnection cn = new SqlConnection("getConnString"))
{
cn.Open();
return cn.GetAll<AddressBook>();
}
}
public int Insert()
{
using (IDbConnection cn = new SqlConnection("getConnString"))
{
cn.Open();
return (int)cn.Insert(this);
}
}
public bool Update()
{
using (IDbConnection cn = new SqlConnection("getConnString"))
{
cn.Open();
return cn.Update(this);
}
}
public bool Delete()
{
using (IDbConnection cn = new SqlConnection("getConnString"))
{
cn.Open();
return cn.Delete(this);
}
}
}
[Table("dbo.distribution")]
public partial class Distribution
{
[Dapper.Contrib.Extensions.Key]
public int Key { get; set; }
public int EntryID { get; set; }
public string Brand { get; set; }
public string Location_Mnemonic { get; set; }
public int Location_Code_Numeric { get; set; }
public string Division_Mnemonic { get; set; }
public string Region_Mnemonic { get; set; }
public string Zone_Mnemonic { get; set; }
public string District_Mnemonic { get; set; }
//similar CRUD methods to AddressBook follow here
}
}
然后使用像这样的 GridView:
<asp:GridView ID="gvAddresses" runat="server" AutoGenerateColumns="true" DataKeyNames="EntryID">
</asp:GridView>
您可以在后面的代码中加载它(并添加 lambda 表达式以进行预排序):
gvAddresses.DataSource = Jacrys.AddressBook.GetAll().OrderBy(c=>c.Last_Name);
您需要的任何下拉菜单都可以通过类似方式加载。
一切都取决于您的需求。如果您有一个分布的网格视图,那么您可以在编辑模式下(在行数据绑定事件中)添加地址下拉列表。当您去更新和保存时,您必须在行中找到控件并在保存记录之前解析它的选定值。真的没有一种很好的单一方法可以完成这项工作。但是,如果您使用 CRUD 方法设置了所有业务类,则可以更简单地将它们连接在一起。