简体   繁体   English

违反多重性约束。 角色 &#39; <T> 关系的&#39;具有多重性1或0..1

[英]Multiplicity constraint violated. The role '<T>' of the relationship has multiplicity 1 or 0..1

I have 1 to many relationship with in my database I am trying to update all of th objects in the collection using a generic method. 我的数据库与我有1对多的关系,我正在尝试使用通用方法更新集合中的所有th对象。

    public static void DuplicateItem<T>(T dataBaseItem, T origionalItem)
    {
        var type = origionalItem.GetType();
        var properties = type.GetProperties();
        var databasePropertyInfo = dataBaseItem.GetType();
        foreach (var origionalProperty in properties)
        {
            var n = origionalProperty.Name;
            var databaseProperty = databasePropertyInfo.GetProperty(origionalProperty.Name);
            if(IsComplex(origionalProperty.GetValue(origionalItem,null)))
                continue;

            var origionalValue = origionalProperty.GetValue(origionalItem, null);
            var t = Nullable.GetUnderlyingType(origionalProperty.PropertyType) 
                ?? origionalProperty.PropertyType;
            var saveValue = (origionalValue == null) ? null : Convert.ChangeType(origionalValue, t);
            databaseProperty.SetValue(dataBaseItem, saveValue);
        }
    }

this method works great except on collections. 除了在集合上,此方法非常有用。 it will just simply add the items from originalItem.Collection to databaseItem.Colletion which is causing me to have double the amount of items that i should which is making me through this excption. 它将只是简单地将项目从originalItem.Collection添加到databaseItem.Colletion ,这使我的项目数量翻了一番,这使我可以通过此操作。 What i want is to just update all of the children from databaseItem.Collection to origanalItem.Collection . 我想要的只是将所有子项从databaseItem.Collection更新为origanalItem.Collection

So I tried to make a change: 因此,我尝试进行更改:

    public static void DuplicateItem<T>(T dataBaseItem, T origionalItem)
    {
        var type = origionalItem.GetType();
        var properties = type.GetProperties();
        var databasePropertyInfo = dataBaseItem.GetType();
        foreach (var origionalProperty in properties)
        {
            var n = origionalProperty.Name;
            var databaseProperty = databasePropertyInfo.GetProperty(origionalProperty.Name);
            if(IsComplex(origionalProperty.GetValue(origionalItem,null)))
                continue;
            if (IsCollection(origionalProperty.GetValue(origionalItem, null)))
            {
                ClearItems(databaseProperty.GetValue(dataBaseItem,null), 
                    origionalProperty.GetValue(origionalItem, null));
            }
            var origionalValue = origionalProperty.GetValue(origionalItem, null);
            var t = Nullable.GetUnderlyingType(origionalProperty.PropertyType) 
                ?? origionalProperty.PropertyType;
            var saveValue = (origionalValue == null) ? null : Convert.ChangeType(origionalValue, t);
            databaseProperty.SetValue(dataBaseItem, saveValue);
        }
    }

and ClearItems: 和ClearItems:

    private static void ClearItems(object databaseValue, object newvalue)
    {
        var l = databaseValue as IEnumerable<object>;
        var n = newvalue as IEnumerable<object>;
        if (l != null)
        {
            for (int i = 0; i < l.Count(); i++)
            {
                var dItem = l.ToList()[i];
                var nItem = n.ToList()[i];
                DuplicateItem(dItem,nItem);
            }

        }
    }

This now gives me only 3 items but i am still throwing the exception. 现在这仅给我3个项目,但我仍然抛出异常。 So now my question: How can i use ClearItems<TDatabase,TOriganial> to update all items in collection and all properties in collection? 所以现在我的问题是:如何使用ClearItems<TDatabase,TOriganial>更新集合中的所有项目以及集合中的所有属性?

Thank you. 谢谢。

I found the solution after a day of playing with it i didn't add a continue so it was still adding the items. 在玩了一天之后,我找到了解决方案,但我没有添加继续,因此它仍在添加项目。

public static void DuplicateItem<T>(T dataBaseItem, T origionalItem)
    {
        var type = origionalItem.GetType();
        var properties = type.GetProperties();
        var databasePropertyInfo = dataBaseItem.GetType();
        foreach (var origionalProperty in properties)
        {
            var n = origionalProperty.Name;
            var databaseProperty = databasePropertyInfo.GetProperty(origionalProperty.Name);
            if(IsComplex(origionalProperty.GetValue(origionalItem,null)))
                continue;
            if (IsCollection(origionalProperty.GetValue(origionalItem, null)))
            {
                ClearItems(databaseProperty.GetValue(dataBaseItem,null), 
                    origionalProperty.GetValue(origionalItem, null));
                continue;
            }
            var origionalValue = origionalProperty.GetValue(origionalItem, null);
            var t = Nullable.GetUnderlyingType(origionalProperty.PropertyType) 
                ?? origionalProperty.PropertyType;
            var saveValue = (origionalValue == null) ? null : Convert.ChangeType(origionalValue, t);
            databaseProperty.SetValue(dataBaseItem, saveValue);
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 违反了EF多重性约束。 关系的角色…具有多重性1或0..1 - EF Multiplicity constraint violated. The role … of the relationship … has multiplicity 1 or 0..1 违反多重性约束。 关系Child_Parent的角色Child_Parent_Target具有多重性1或0..1 - Multiplicity constraint violated. The role Child_Parent_Target of the relationship Child_Parent has multiplicity 1 or 0..1 违反多重性约束。 关系“ LibarySystem.DataModel”的角色“ Student_LendedBooks…”具有多重性1或0..1 - Multiplicity constraint violated. The role 'Student_LendedBooks…' of the relationship 'LibarySystem.DataModel' has multiplicity 1 or 0..1 违反多重性约束。 关系xxx的角色xxx具有多重性1或0..1 - Multiplicity constraint violated. The role xxx of the relationship xxx has multiplicity 1 or 0..1 EF6 数据库优先:违反多重性约束。 关系“...”的角色“...”具有多重性 1 或 0..1 - EF6 Database-First: Multiplicity constraint violated. The role '…' of the relationship '…' has multiplicity 1 or 0..1 &#39;违反了多重约束。 关系 Rayon_Produits&#39; 的角色 &#39;Rayon_Produits_Source&#39; 具有多重性 1 或 0..1。 - 'Multiplicity constraint violated. The role 'Rayon_Produits_Source' of the relationship Rayon_Produits' has multiplicity 1 or 0..1.' 违反了多重约束。 “Repository.Person_Projects”关系的“Person_Projects_Source”角色具有多重性1或0..1 - Multiplicity constraint violated. The role 'Person_Projects_Source' of the relationship 'Repository.Person_Projects' has multiplicity 1 or 0..1 EF 6.0代码优先:违反多重性约束。 关系的作用 - EF 6.0 Code First: Multiplicity constraint violated. the role of the relationship 主要角色的多重性必须为0..1 - Multiplicity of the Principal Role must be 0..1 违反多重性约束的实体框架6 - Multiplicity constraint violated Entity framework 6
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM