1

我研究过使用 EF、nHibernate 和 Dapper/Dapper.SimpleCRUD。在他们中,我无法弄清楚如何代表我的数据库(SQL Server 2012)模型的用例。我正在使用 C# 4.0/.NET 4.0 中的网格(由于技术限制)构建一个 ASP.NET 网站,该网站将具有 CRUD 功能,网格的初始状态由下拉菜单设置。

我的两个表是这样设置的:

Address_Book 
 |_[EntryID]
 |_[Last_Name]
 |_[First_Name]
 |_[Title]
 |_[Office_Num]
 |_[Cell_Num]
 |_[Home_Num]
 |_[Email_Address]
 |_[Special_Info]
 |_[hr24_Emails]
 |_[hr48_Emails]
 |_[RM_Emails]
 |_[Prestige_Emails]
 |_[GEB_Emails]
 |_[LAW_Emails]

Distribution
 |_[Brand]
 |_[Location_Mnemonic]
 |_[Location_Code_Numeric]
 |_[EntryID]
 |_[Division_Mnemonic]
 |_[Region_Mnemonic]
 |_[Zone_Mnemonic]
 |_[District_Mnemonic]
 |_[Key]

Distribution 和 Address_Book 之间存在多对一关系,其中 Address_book.EntryID = Distribution.EntryID。

任何有关如何设置的帮助将不胜感激。我在手动管理 CRUD 操作时遇到问题,所以我认为 ORM 会有所帮助,但我无法弄清楚。任何帮助表示赞赏。

提前致谢!

4

1 回答 1

0

整个 .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 方法设置了所有业务类,则可以更简单地将它们连接在一起。

于 2017-02-17T22:37:49.703 回答