[英]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.