在查询中使用连接的任何示例都适用于npoco

时间:2016-02-04 14:15:53

标签: npoco

你能给我一些从连接表中查询和映射结果集的例子吗?我有以下代码:

DTO:

public class FormCatalogRecord
{
    [Column("MFormId")]
    public int Id { get; set; }

    [Column("SGroupId")]
    public int? GroupId { get; set; }

    [Column("SGroupName")]
    public string GroupName { get; set; }

    [Column("STypeFormId")]
    public int TypeId { get; set; }

    [Column("MFormKod")]
    public string Code { get; set; }

    [Column("MFormName")]
    public string Name { get; set; }

    [Column("SFormPeriodName")]
    public string Period { get; set; }

    [Column("MFormDateStart")]
    public DateTime? DateStarts { get; set; }

    [Column("MFormDateEnd")]
    public DateTime? DateEnds { get; set; }

    [Column("MFormPublish")]
    public DateTime? PublishDate { get; set; }
}

查询:

string formsQueryStr =
                    "FROM MForm " +
                    "INNER JOIN SFormPeriod ON MForm.SFormPeriodId = SFormPeriod.SFormPeriodId " +
                    "INNER JOIN SGroup ON MForm.SGroupId = SGroup.SGroupId " +
                    "ORDER BY MForm.SGroupId";

var test = db.Fetch<FormCatalogRecord>(formsQueryStr);

结果是异常:“不明确的列名'SGroupId'”。 我试图改变属性:

[Column("MForm.SGroupId")]
public int? GroupId { get; set; }

现在我得到一个例外

  

无效的列名称'MForm.SGroupId'

令人惊讶的是,我在官方文档中没有找到任何示例。任何对npoco有效的帮助都会有用。

更新

对于我的第一个案例,我正在关注SQL生成:

SELECT 
    [MFormId] AS [Id], [SGroupId] AS [GroupId],
    [SGroupName] AS [GroupName], [STypeFormId] AS [TypeId], 
    [MFormKod] AS [Code], [MFormName] AS [Name], 
    [SFormPeriodName] AS [Period], [MFormDateStart] AS [DateStarts], 
    [MFormDateEnd] AS [DateEnds], [MFormPublish] AS [PublishDate] 
FROM 
    MForm 
INNER JOIN 
    SFormPeriod ON MForm.SFormPeriodId = SFormPeriod.SFormPeriodId 
INNER JOIN 
    SGroup ON MForm.SGroupId = SGroup.SGroupId 
ORDER BY 
    MForm.SGroupId

我想我需要一些方法来添加“MForm”。在“[SGroupId] AS [GroupId]”之前。如果列属性可以支持[Column(“TableName.ColumnName”)]

之类的语法,那将会很好

1 个答案:

答案 0 :(得分:0)

NPooc和PetaPoco现在是不同的项目,现在很难将它们与其他项目联系起来。

至于你的问题。你试过吗

string formsQueryStr = 
                    "SELECT MForm.*" +
                    "FROM MForm " +
                    "INNER JOIN SFormPeriod ON MForm.SFormPeriodId = SFormPeriod.SFormPeriodId " +
                    "INNER JOIN SGroup ON MForm.SGroupId = SGroup.SGroupId " +
                    "ORDER BY MForm.SGroupId";

var test = db.Fetch<FormCatalogRecord>(formsQueryStr);