Guest User

Untitled

a guest
Oct 18th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. using Microservices.LibCore.Core;
  2. using Microservices.LibCore.Core.Base.Models;
  3. using Microsoft.EntityFrameworkCore;
  4. using Newtonsoft.Json;
  5. using NLog;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.ComponentModel.DataAnnotations;
  9. using System.ComponentModel.DataAnnotations.Schema;
  10. using System.Data.SqlClient;
  11. using System.Linq;
  12. using System.Net;
  13. using System.Reflection;
  14.  
  15. public static List<object> setLegacyKey(DbContext dbContext, ServiceObject obj)
  16. {
  17. var keyValuesFound = new List<object>();
  18. var keyProperties = getKeyProperties(obj);
  19.  
  20. foreach (var key in keyProperties)
  21. {
  22. string sql = "select " + getKeyColumnName(key) + " from " + getTableName(obj) + " where global_id=@p0"; //todo: while the risk is low, add some validation to prevent SQL injection
  23.  
  24. List<object> result = dbContext.Database.SqlQuery(key.PropertyType, sql, obj.Id).ToListAsync().Result;
  25.  
  26. if (result.Count == 0)
  27. {
  28. throw new ServiceException(ServiceError.ERROR_CODE.INVALID_REQUEST, String.Format("Invalid {1}.ID ({0})", obj.Id, obj.GetType().Name));
  29. }
  30.  
  31. var keyValue = result.FirstOrDefault();
  32.  
  33. key.SetValue(obj, keyValue);
  34.  
  35. keyValuesFound.Add(keyValue);
  36. }
  37.  
  38. return keyValuesFound;
  39. }
Add Comment
Please, Sign In to add comment