gpt4 book ai didi

c# - Dapper 复杂映射 Dapper.Extensions Dapper.FluentMap

转载 作者:行者123 更新时间:2023-11-30 20:45:34 29 4
gpt4 key购买 nike

我的代码有点问题,我想让 dapper 很好地使用它。

当我说我的代码是继承的,所以这不是我的设计。

我正在尝试替换 Entity Framework,因为对数据库的调用效率不高,所以我想更好地控制生成的 SQL,因此 Dapper 似乎是显而易见的选择。

我遇到的问题是我正在努力将我拥有的 poco 类映射到 dapper 多查询。

我遇到的问题如下:

    public Feed GetFeedDapper(int feedId)
{
Feed feed = null;

var sql =
@"
SELECT * FROM Feeds WHERE FeedId= @FeedId
SELECT * FROM FeedFilterParameters WHERE FeedId = @FeedId
SELECT * FROM TeamFeeds WHERE FeedId = @FeedId";

using (var multi = DbConnection.QueryMultiple(sql, new { FeedId = feedId }))
{
feed = multi.Read<Feed>().Single();
feed.Parameters = multi.Read<FeedFilterParameter>().ToList();
feed.TeamFeeds = multi.Read<TeamFeed>().ToList();
}

return feed;
}

这让我从数据库中获得了正确的结果,这很好,问题是并非 Feed 对象上的所有属性都被映射。提要有一个名为 InboundProperties 的属性,其类型为 InboundProperties,如下所示,在数据库中它们存储为 InboundProperties_{PropName}。这些属性没有被映射,我在 DapperExtensions 或 FluentMap 中尝试的任何东西都不起作用。

public class InboundProperties
{
public string ExternalRef { get; set; }
public string ExternalRefPrevious { get; set; }
public string ExternalId { get; set; }
public string ExternalName { get; set; }
public string ExternalToken { get; set; }
public int ExternalAPICounts { get; set; }
public string ExternalLink { get; set; }
public string ExternalPicture { get; set; }
public string LastProcessedMessageId { get; set; }
public long? LastProcessedMessageTime { get; set; }
public DateTime? MessageCountStartDT { get; set; }
public Int32 TenancyId { get; set; }
public virtual int FeedScopeInt { get; set; }
}

谁能帮我映射这些属性??

最佳答案

这就是我解决它的方法,但接受了#rraszewski 的回答,因为它也有效,但这只是意味着我需要以非常手动的方式处理所有选择。

public Feed GetFeedDapper(int feedId)
{
Feed feed = null;

var multiPredicate = new GetMultiplePredicate();
multiPredicate.Add<Feed>(Predicates.Field<Feed>(x => x.FeedId, Operator.Eq, feedId));
multiPredicate.Add<InboundProperties>(Predicates.Field<InboundProperties>(x => x.FeedId, Operator.Eq, feedId));
multiPredicate.Add<OutboundProperties>(Predicates.Field<OutboundProperties>(x => x.FeedId, Operator.Eq, feedId));
multiPredicate.Add<FeedFilterParameter>(Predicates.Field<FeedFilterParameter>(x => x.FeedId, Operator.Eq, feedId));
multiPredicate.Add<TeamFeed>(Predicates.Field<TeamFeed>(x => x.FeedId, Operator.Eq, feedId));

var result = DbConnection.GetMultiple(multiPredicate);

feed = result.Read<Feed>().Single();
feed.InboundProperties = result.Read<InboundProperties>().Single();
feed.OutboundProperties = result.Read<OutboundProperties>().Single();
feed.Parameters = result.Read<FeedFilterParameter>().ToList();
feed.TeamFeeds = result.Read<TeamFeed>().ToList();

return feed;
}

然后我映射类:

public class FeedMapper : ClassMapper<Feed>
{
public FeedMapper()
{
base.Table("Feeds");
Map(f => f.FeedId).Key(KeyType.Identity);

Map(f => f.Owner).Ignore();
Map(f => f.TeamFeeds).Ignore();
Map(f => f.FeedDirection).Ignore();
Map(f => f.InboundProperties).Ignore();
Map(f => f.Parameters).Ignore();
Map(f => f.OutboundProperties).Ignore();
Map(f => f.RelatedTeams).Ignore();

AutoMap();
}
}

public class InboundPropertiesMapper : ClassMapper<InboundProperties>
{
public InboundPropertiesMapper()
{
base.Table("Feeds");
Map(f => f.FeedId).Key(KeyType.Identity);

Map(f => f.Channel).Ignore();
Map(f => f.FeedScope).Ignore();

Map(f => f.ChannelInt).Column("InboundProperties_ChannelInt");
Map(f => f.ExternalAPICounts).Column("InboundProperties_ExternalAPICounts");
Map(f => f.ExternalId).Column("InboundProperties_ExternalId");
Map(f => f.ExternalLink).Column("InboundProperties_ExternalLink");
Map(f => f.ExternalName).Column("InboundProperties_ExternalName");
Map(f => f.ExternalPicture).Column("InboundProperties_ExternalPicture");
Map(f => f.ExternalRef).Column("InboundProperties_ExternalRef");
Map(f => f.ExternalRefPrevious).Column("InboundProperties_ExternalRefPrevious");
Map(f => f.ExternalToken).Column("InboundProperties_ExternalToken");
Map(f => f.FeedScopeInt).Column("InboundProperties_FeedScopeInt");
Map(f => f.LastProcessedMessageId).Column("InboundProperties_LastProcessedMessageId");
Map(f => f.LastProcessedMessageTime).Column("InboundProperties_LastProcessedMessageTime");
Map(f => f.MessageCountStartDT).Column("InboundProperties_MessageCountStartDT");
Map(f => f.TenancyId).Column("InboundProperties_TenancyId");
AutoMap();
}
}

public class OutboundPropertiesMapper : ClassMapper<OutboundProperties>
{
public OutboundPropertiesMapper()
{
base.Table("Feeds");
Map(f => f.FeedId).Key(KeyType.Identity);

Map(f => f.Channel).Ignore();
Map(f => f.FeedType).Ignore();

Map(f => f.OutboundFeedTypeInt).Column("OutboundProperties_OutboundFeedTypeInt");
Map(f => f.TenancyId).Column("OutboundProperties_TenancyId");
AutoMap();
}
}

关于c# - Dapper 复杂映射 Dapper.Extensions Dapper.FluentMap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28046528/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com