-
[EFCore] Grpc Data ParsingC#/EFCore 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'C# > EFCore' 카테고리의 다른 글
[EFCore] reset sqlite_sequence (0) 2023.10.12 [EFCore] Key 값 넣을 때 (1) 2023.10.06 [EFCore] SQLite .db 사용시 Close (0) 2023.08.29 [EFCore] 시작하기 (0) 2022.11.09 댓글