[英]Ef Core - Use Automapper to get data from keyless View
我需要知道是否可以使用 Automapper 從數據庫視圖映射 DTO 屬性。 或者也許在 DbContext 模型配置中是可能的。 讓我們想象一下,我有一個包含其他相關數據的視圖的商業目的,但為了簡潔起見,我使類變得簡單
相關的掘金
我有一個實體
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運行示例代碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.