C# 数据库列映射到对象
我用的是我们的微型ORM。我想弄清楚是否有办法将以下POCO类关联和结果从一行映射到这些对象中C# 数据库列映射到对象,c#,sql,orm,insight.database,C#,Sql,Orm,Insight.database,我用的是我们的微型ORM。我想弄清楚是否有办法将以下POCO类关联和结果从一行映射到这些对象中 public class Rule { public int Id { get; set; } public string Name { get; set; } public RuleDetail Source { get; set; } public RuleDetail Destination { get; set; } } public class RuleDe
public class Rule
{
public int Id { get; set; }
public string Name { get; set; }
public RuleDetail Source { get; set; }
public RuleDetail Destination { get; set; }
}
public class RuleDetail
{
public int Id { get; set; }
public Name { get; set; }
public Date DateTime { get; set; }
// omitted...
}
以下是从存储过程返回的列:
Id
Name
// Should map to Source object.
SourceId
SourceName
SourceDateTime
// Should map to Destination object.
DestinationId
DestinationName
DestinationDateTime
你可以试试
public interface IRepo
{
[Recordset(1, typeof(RuleDetail), into="Source", IsChild=true)]
[Recordset(2, typeof(RuleDetail), into="Destination", IsChild=true)]
Rule GetFullyPopulatedRuleByIdStoredProcedure(int id);
}
您需要返回三个记录集-第一个[记录集(0)]是您的规则,另外两个包含源和目标。我经常使用这种安排,它对我很有效
如果要在单个记录集中返回单个行,我不知道有什么方法可以做到这一点。在查询端进行一点设置就可以做到这一点。不确定是否可以使用属性
var returns = Query.Returns(
new OneToOne<Rule, RuleDetail, RuleDetail>(
callback: (rule, source, destination) => {
rule.Source = source;
rule.Destination = destination;
},
columnOverride: new ColumnOverride[] {
new ColumnOverride<RuleDetail>("SourceId", "Id"),
new ColumnOverride<RuleDetail>("SourceName", "Name"),
new ColumnOverride<RuleDetail>("SourceDateTime", "DateTime"),
new ColumnOverride<RuleDetail>("DestinationId", "Id"),
new ColumnOverride<RuleDetail>("DestinationName", "Name"),
new ColumnOverride<RuleDetail>("DestinationDateTime", "DateTime"),
},
splitColumns: new Dictionary<Type, string>() {
{ typeof(Rule), "Id" },
{ typeof(RuleDetail), "SourceId" },
{ typeof(RuleDetail), "DestinationId" },
}
)
);
return Db.Query(procedure, params, returns);
var returns=Query.returns(
新奥涅托内(
回调:(规则、源、目标)=>{
规则.来源=来源;
规则.目的地=目的地;
},
columnOverride:新columnOverride[]{
新的ColumnOverride(“SourceId”、“Id”),
新ColumnOverride(“SourceName”、“Name”),
新列覆盖(“SourceDateTime”、“DateTime”),
新列覆盖(“DestinationId”、“Id”),
新列覆盖(“DestinationName”、“Name”),
新列覆盖(“DestinationDateTime”、“DateTime”),
},
splitColumns:newdictionary(){
{typeof(规则),“Id”},
{typeof(RuleDetail),“SourceId”},
{typeof(RuleDetail),“DestinationId”},
}
)
);
return Db.Query(过程、参数、返回);
我不确定是否需要所有这些代码才能使其工作,但它确实显示了Insight.Database中的查询功能有多强大