如何解决Ef Core-使用Automapper从无键视图获取数据
我需要知道是否有可能使用Automapper从数据库视图映射DTO属性。也许在DbContext模型配置中是可能的。假设我有一个包含其他相关数据的视图是一个商业目的,但是为了简洁起见,我使类变得简单
相关Nugets
- EF Core v3.1.7
- AutoMapper v10.0.0
我有一个实体
public class Foo {
public int Id { get; set; }
public string Name { get; set; }
}
我有一个无钥匙视图
public class BarVW {
public int FooId { get; set; }
public string FooName { get; set; }
}
该视图内置于DB Initializer类中
context.Database.ExecuteSqlRaw(
@"CREATE OR REPLACE VIEW v_Bar AS
select
f.Id as FooId,f.[Name] as FooName
from
Foo f
-- where some logic that makes a Foo appear in view"
);
然后将视图分配给DbContext类中的DbSet
modelBuilder.Entity<BarVW>(eb =>
{
eb.HasNoKey();
eb.ToView("v_Bar");
});
public DbSet<BarVW> BarVW { get; set; }
在我的FooDto中,我需要为实体类添加一个附加属性,以指示该Foo存在于数据库视图v_Bar
中。public class FooDto {
public int Id { get; set; }
public string Name { get; set; }
public bool IsBar { get; set; }
}
对于Automapper配置,我有以下内容,但是我不知道有什么方法可以从BarVW DbSet映射dto IsBar
属性
CreateMap<Foo,FooDto>()
.ForMember(dto => dto.IsBar,opt => ??? ); // don't know what to put here
解决方法
完成所需操作的一种简单方法是引入导航属性(例如Bars
)并将其用于映射配置(例如opt.MapFrom(src => src.Bars.Any())
)。
这是一个可以正常运行的示例控制台程序,它演示了这种方法:
using System.Diagnostics;
using System.Linq;
using AutoMapper;
using AutoMapper.QueryableExtensions;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
namespace IssueConsoleTemplate
{
public class Foo
{
public int Id { get; set; }
public string Name { get; set; }
public BarVW Bar { get; set; }
}
public class BarVW
{
public int FooId { get; set; }
public string FooName { get; set; }
public Foo Foo { get; set; }
}
public class Context : DbContext
{
public virtual DbSet<Foo> Foo { get; set; }
public virtual DbSet<BarVW> BarVW { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder
.UseSqlServer(
@"Data Source=.\MSSQL14;Integrated Security=SSPI;Initial Catalog=So63850736")
.UseLoggerFactory(LoggerFactory.Create(b => b
.AddConsole()
.AddFilter(level => level >= LogLevel.Information)))
.EnableSensitiveDataLogging()
.EnableDetailedErrors();
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Foo>()
.HasData(
new Foo {Id = 1,Name = "Fo"},new Foo {Id = 2,Name = "Foo"},new Foo {Id = 3,Name = "Fooo"});
modelBuilder.Entity<BarVW>(
eb =>
{
eb.HasKey(e => e.FooId);
eb.ToView("v_Bar");
eb.HasOne(e => e.Foo)
.WithOne(e => e.Bar)
.HasForeignKey<BarVW>(e => e.FooId);
});
}
}
public class FooDto
{
public int Id { get; set; }
public string Name { get; set; }
public bool IsBar { get; set; }
}
internal static class Program
{
private static void Main()
{
using var context = new Context();
context.Database.EnsureDeleted();
context.Database.EnsureCreated();
context.Database.ExecuteSqlRaw(
@"CREATE VIEW [v_Bar] AS
select
f.[Id] as [FooId],f.[Name] as [FooName]
from
[Foo] f
where
f.[Id] >= 1 and f.[Id] <= 2"
);
var config = new MapperConfiguration(
cfg => cfg
.CreateMap<Foo,FooDto>()
.ForMember(dto => dto.IsBar,opt => opt.MapFrom(src => src.Bar != null)));
var result = context.Foo
.ProjectTo<FooDto>(config)
.ToList();
Debug.Assert(result.Count == 3);
Debug.Assert(result.Count(dto => dto.IsBar) == 2);
}
}
}
为查询生成的SQL如下所示:
SELECT [f].[Id],CASE
WHEN [v].[FooId] IS NOT NULL THEN CAST(1 AS bit)
ELSE CAST(0 AS bit)
END AS [IsBar],[f].[Name]
FROM [Foo] AS [f]
LEFT JOIN [v_Bar] AS [v] ON [f].[Id] = [v].[FooId]
您可以使用.NET Fiddle运行示例代码。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。