是否可以从源对象映射到源对象不同的目标对象? 配置表通过 Code 属性具有与查找表相关的对象(一种松散耦合场景) 由于遗留原因,源对象中不能存在 ServiceLookup 和 ClassificationLookup。
这些是表格。
CREATE TABLE Config ( Id INT IDENTITY (1, 1) NOT NULL
, Code NVARCHAR (50) NOT NULL
, ServiceLookupCode NVARCHAR (50) NOT NULL
, ClassificationLookupCode NVARCHAR (50) NOT NULL
, SomeConfigValue NVARCHAR (50) NOT NULL
, CONSTRAINT PK_Table1 PRIMARY KEY CLUSTERED (Id ASC)
, CONSTRAINT UQ_Table1_Code UNIQUE NONCLUSTERED (Code ASC) );
CREATE TABLE ServiceLookup ( Id int IDENTITY(1,1) NOT NULL
, Code nvarchar(50) NOT NULL
, Name nvarchar(255) NOT NULL
, CONSTRAINT PK_ServiceLookup PRIMARY KEY CLUSTERED (Id ASC)
, CONSTRAINT UQ_ServiceLookup_Code UNIQUE NONCLUSTERED (Code ASC) );
CREATE TABLE ClassificationLookup ( Id int IDENTITY(1,1) NOT NULL
, Code nvarchar(50) NOT NULL
, Name nvarchar(255) NOT NULL
, CONSTRAINT PK_ClassificationLookup PRIMARY KEY CLUSTERED (Id ASC)
, CONSTRAINT UQ_ClassificationLookup_Code UNIQUE NONCLUSTERED (Code ASC) );
返回数据的SQL示例
SELECT c.Id, c.Code, c.SomeConfigValue
, sl.Name AS ServiceName
, cl.Name AS ClassificationName
FROM Config c
INNER JOIN ServiceLookup sl ON c.ServiceLookupCode = sl.Code
INNER JOIN ClassificationLookup cl ON c.ClassificationLookupCode = cl.Code
实体 ServiceLookup 和 ClassificationLookup 表无法在 Config 表中定义(遗留原因)
public class Config
{
public int Id { get; set; }
public string Code { get; set; }
public string ServiceLookupCode { get; set; }
public string ClassificationLookupCode { get; set; }
public string SomeConfigValue { get; set; }
//public ServiceLookup ServiceLookup { get; set; } <-- This is not allowed due to loose coupling design (legacy reasons)
//public ClassificationLookup ServiceLookup { get; set; } <-- This is not allowed due to loose coupling design (legacy reasons)
}
public class ServiceLookup
{
public int Id { get; set; }
public string Code { get; set; }
public string Name { get; set; }
}
public class ClassificationLookup
{
public int Id { get; set; }
public string Code { get; set; }
public string Name { get; set; }
}
DTO
public class ConfigDto
{
public int Id { get; set; }
public string Code { get; set; }
public string ServiceLookupCode { get; set; }
public string ClassificationLookupCode { get; set; }
public string SomeConfigValue { get; set; }
public LookupModel ServiceLookup { get; set; }
public LookupModel ClassificationLookup { get; set; }
public class LookupModel
{
public int Id { get; set; }
public string Code { get; set; }
public string Name { get; set; }
}
}
Linq 代码
var result = await (from c in Db.Config
join sl in Db.ServiceLookup on c.ServiceLookupCode equals dl.Code
join cl in Db.ClassificationLookup on c.ClassificationLookupCode equals cl.Code
select new ConfigDto
{
Id = c.Id,
Code = c.Code,
SomeConfigValue = c.SomeConfigValue,
ServiceLookupCode = c.ServiceLookupCode,
ClassificationLookupCode = c.ClassificationLookupCode,
ServiceLookup = new ConfigDto.LookupModel
{
Id = sl.Id,
CodePath = sl.Code,
Name = sl.Name
},
ClassificationLookup = new ConfigDto.LookupModel
{
Id = cl.Id,
Code = cl.Code,
Name = cl.Name
}
}
)