将数据从2个表映射到单个实体

c# dapper sql

我有2个表有一对一的关系。表格如下:

CREATE TABLE [dbo].[Provider](
[ProviderId] [int] IDENTITY(1,1) PRIMARY KEY NOT NULL,
[FirstName] [varchar](40) NOT NULL,
[LastName] [varchar](40) NOT NULL,
[SSN] [varchar](15) NOT NULL,
[NPI] [varchar](15) NOT NULL,
[ProviderStatus] [bit] NOT NULL)

CREATE TABLE [dbo].[ProviderDetails](
[ProviderDetailsID] [int] IDENTITY(1,1) PRIMARY KEY NOT NULL,
[Certification] [varchar](40) NOT NULL,
[Specialization] [varchar](40) NOT NULL,
[TaxonomyCode] [varchar](40) NOT NULL,
[ContactNumber] [varchar](15) NOT NULL,
[ContactEmail] [varchar](40) NOT NULL,
[ProviderId]  [int] FOREIGN KEY REFERENCES Provider(ProviderId) NOT NULL)

我在C#中创建了2个实体(请注意,我不使用EF)

 public class Provider
{
    public int ProviderID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string SSN { get; set; }
    public string NPI { get; set; }
    public ProviderDetails ProviderDetails { get; set; }
}

public class ProviderDetails
{
    public int ProviderDetailsId { get; set; }
    public string Certification { get; set; }
    public string Specialization { get; set; }
    public string TaxonomyCode { get; set; }
    public string ContactNumber { get; set; }
    public string ContactEmail { get; set; }
    public int ProviderId { get; set; }
}

我,之前有一个实体,提供商。因此,我能够使用以下代码获取要显示的提供程序列表。

    public List<Provider> GetListofProviders()
    {
        List<Provider> Providers = new List<Provider>();
        using (_dbConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["DemoApplicationConnection"].ConnectionString))
        {
            try
            {
                Providers = (List<Provider>)(_dbConnection.Query<Provider>("spGetAllProviders", commandType: CommandType.StoredProcedure));
            }
            catch (Exception)
            {
                throw;
            }
        } 
        return Providers;
    }

要从数据库中获取数据,我使用以下查询:

ALTER PROC [dbo].[spGetAllProviders]
AS
SELECT * FROM Provider 
INNER JOIN ProviderDetails 
ON Provider.ProviderId = ProviderDetails .ProviderId
WHERE Provider.ProviderStatus = 1

由于我已拆分实体,有人可以指导我如何将数据从数据库映射到2个实体吗?

热门答案

现在这不是正确的答案。但我认为这将是可行的方法。虽然它不适合我。所以,如果有人能告诉我我做错了什么,会非常有帮助。

public List<Provider> GetListofProviders()
{
   List<Provider> Providers = new List<Provider>();
   using (_dbConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["DemoApplicationConnection"].ConnectionString))
   {
       try
       {
          Providers = _dbConnection.Query<Provider,ProviderDetails,Provider>("spGetAllProviders",null,splitOn: "ProviderId", commandType: CommandType.StoredProcedure).ToList();
       }
       catch (Exception)
       {
         throw;
       }
   } 
   return Providers;
}

存储过程也是

ALTER PROC [dbo].[spGetAllProviders]
AS
SELECT Provider.*,
       ProviderDetails.ProviderId,
       ProviderDetails.ProviderDetailsID,
       ProviderDetails.Certification,
       ProviderDetails.Specialization,
       ProviderDetails.TaxonomyCode,
       ProviderDetails.ContactNumber,
       ProviderDetails.ContactEmail 
FROM Provider 
INNER JOIN ProviderDetails 
ON Provider.ProviderId = ProviderDetails .ProviderId
WHERE Provider.ProviderStatus = 1

我收到以下错误。

使用多映射API时,如果您具有Id以外的密钥,请确保设置splitOn参数



许可下: CC-BY-SA with attribution
不隶属于 Stack Overflow
这个KB合法吗? 是的,了解原因
许可下: CC-BY-SA with attribution
不隶属于 Stack Overflow
这个KB合法吗? 是的,了解原因