【问题标题】:How to setup my code as DB First in an ORM如何在 ORM 中将我的代码设置为 DB First
【发布时间】:2017-02-16 22:17:47
【问题描述】:

我研究过使用 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 会有所帮助,但我无法弄清楚。任何帮助表示赞赏。

提前致谢!

【问题讨论】:

  • 您最烦恼的是什么?网格视图?设置班级?设置使用下拉菜单作为选择器的 gridivew?
  • 设置映射到逻辑模型的类。我脑子里有它应该如何工作的模型,但是实现它是令人烦恼的。一个地址簿条目可以有多个与之关联的分布,并且提出如何创建一个可以创建新分布以及更新现有分布的类是其中的一部分。
  • 什么是 EntryID?是从另一张桌子上来的吗?一个地址只能有一个Distribution是真的吗?
  • EntryID 是 Address_Book 中的 AutoInc 列。不,一个地址簿可以有多个分布。
  • ...然后一个发行版只能有一个地址(如上面的多对一注释)?

标签: entity-framework visual-studio-2010 c#-4.0 nhibernate dapper-simplecrud


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-09
    • 1970-01-01
    • 2011-04-17
    • 1970-01-01
    • 2013-11-14
    • 2012-09-06
    • 2021-12-05
    • 2012-12-25
    相关资源
    最近更新 更多