Как заменить ObjectContext в Entity Framework Core 3.1 - PullRequest
1 голос
/ 04 февраля 2020

Мне было поручено перенести проект, написанный на Entity Framework 6, на Entity Framework Core 3.1.

Большую часть работы я проделал, но не могу найти решение для замены ObjectContext. В настоящий момент я сталкиваюсь с такой проблемой:

    public static T FromObjectToStruct<T>(object o) where T : struct
    {
        // Cast the value read to type choice
        return (T) (o as T?).GetValueOrDefault();
    }


    /// <summary>
    /// Convert an object to a dictionary
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="obj"></param>
    /// <returns></returns>
    public static Dictionary<string, object> ToDictionary<T>(this T obj) where T : class
    {
        var dictionary = new Dictionary<string, object>();
        var properties = ObjectContext.GetObjectType(obj.GetType()).GetProperties(
            BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.FlattenHierarchy);
        foreach (var p in properties)
        {
            dictionary.Add(p.Name, p.GetValue(obj, null));
        }

        return dictionary;
    }

    /// <summary>
    /// Returns the differences between two objects of the same type T
    /// </summary>
    /// <param name="obj1"></param>
    /// <param name="obj2"></param>
    /// <returns></returns>
    public static Dictionary<string, object> GetDiff<T>(this T obj1, T obj2) where T : class
    {
        var diff = new Dictionary<string, object>();
        var objType = ObjectContext.GetObjectType(obj1.GetType());
        var objProperties = objType.GetProperties(BindingFlags.Instance | BindingFlags.NonPublic |
                                                  BindingFlags.Public | BindingFlags.FlattenHierarchy);

        foreach (var objProperty in objProperties)
        {
            var objValue1 = objProperty.GetValue(obj1, null);
            var objValue2 = objProperty.GetValue(obj2, null);
            if (!Equals(objValue1, objValue2))
            {
                diff.Add(objProperty.Name, objValue2);
            }
        }

        return diff;
    }

Любая подсказка или ссылка на документацию, которая объясняет, как обойти проблему, было бы замечательно.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...