C#/EFCore
[EFCore] Grpc Data Parsing
딸기우유중독
2023. 9. 21. 16:07
DbContext로 db 에서 불러온 Data Pasing할 때 외래키로 들고 있는 DataType포함 하여 쿼리하는 방법.
public static List<TagInfo> ReadAllTagDB(string TagDbName = null)
{
using var tagContext = new TagDbContext();
try
{
//tagContext.DbPath = Path.Combine(DbWorkingPath, TagDbName + ".db");
//var findTag = tagContext.Tags.Any(x => x.Id == TagId);
//var readTag = tagContext.Tags.Where(x => x.Id == TagId).FirstOrDefault(); //Include(r=>r.DeviceInfomation).Where(r=>r.Id == "Hello").FirstOrDefault();
//TagInfo tagInfo = null;
var tagList = new List<TagInfo>();
var dbList = tagContext.Tags.Select(x => x)
.Include(x=>x.DeviceDataGuid)
.Include(x=>x.DeviceDataGuid.DeviceTypeDataId)
.Include(x=>x.DeviceDataGuid.DeviceStatusDataId)
.Include(x=>x.TagTypeDataId)
.ToList();
foreach (var tag in tagContext.Tags.Select(x=>x).ToList())
{
//var dev = tagContext.Devices.Where(x => x.DeviceGuid == tag.ProducedDevice).FirstOrDefault();
var deviceStatus = new DeviceStatus()
{
Id = tag.DeviceDataGuid.DeviceStatusDataId.Id,
Korean = tag.DeviceDataGuid.DeviceStatusDataId.Korean,
English = tag.DeviceDataGuid.DeviceStatusDataId.English,
Chinese = tag.DeviceDataGuid.DeviceStatusDataId.Chinese
};
var deviceType = new DeviceType()
{
Id = tag.DeviceDataGuid.DeviceTypeDataId.Id,
Name = tag.DeviceDataGuid.DeviceTypeDataId.Name
};
var device = new Device()
{
//Id = tag.DeviceDataGuid.Id,
DeviceGuid = tag.DeviceDataGuid.DeviceGuid,
Name = tag.DeviceDataGuid.Name,
Status = deviceStatus,
Type = deviceType
};
var tagType = new TagType()
{
Id = tag.TagTypeDataId.Id,
Name = tag.TagTypeDataId.Name
};
var tagInfo = new TagInfo()
{
Id = tag.Id,
Name = tag.Name,
Type = tagType,
Comment = tag.Comment,
Address = tag.Address,
Device = device,
};
tagList.Add(tagInfo);
}
if (tagContext.Database.GetDbConnection() is SqliteConnection conn)
{
SqliteConnection.ClearPool(conn);
}
return tagList;
}
catch (Exception e)
{
if (tagContext.Database.GetDbConnection() is SqliteConnection conn)
{
SqliteConnection.ClearPool(conn);
}
Console.WriteLine(e);
throw;
}
}
728x90