我正在开发Web应用程序的保存配置文件部分(在ASP.NET / C#中),但是,我发现了一个"模糊的匹配"每当代码尝试通过在运行时调用每个" AddObject"来保存新的配置文件时都会出现异常。代码部分如下:
using System;
using System.ComponentModel.Composition;
using System.Linq;
using Itok.DataAccess.Interfaces;
using System.Collections.Generic;
using Itok.Entities;
namespace Itok.DataAccess.Repositories
{
[Export(typeof(IProfileRepository))]
public class ProfileRepository : IProfileRepository
{
private Connection conn;
public Int32 SaveProfile(Profile profile)
{
Int32 profileID1 = 0;
Boolean HasAttributeId = false;
profile.LastUpdateDate = DateTime.Now;
using(ItokDataContext dc = conn.GetContext())
{
if (profile.ProfileID > 0)
{
dc.Profiles.Attach(new Profile { ProfileID = profile.ProfileID });
dc.Profiles.ApplyCurrentValues(profile);
if (profile.Attributes != null)
{
foreach(ProfileAttribute item in profile.Attributes)
{
dc.ProfileAttributes.Attach(new ProfileAttribute
{ ProfileAttributeID = item.ProfileAttributeID,
});
dc.ProfileAttributes.ApplyCurrentValues(item);
}
}
}
else
{
profile.CreateDate = DateTime.Now;
//when working with new profile we don't expect attributes to be null, but check anyway
if (profile.Attributes != null)
{
foreach (ProfileAttribute item in profile.Attributes)
{
dc.ProfileAttributes.AddObject(new ProfileAttribute
{
ProfileAttributeID = item.ProfileAttributeID,
ProfileAttributeTypeID = item.ProfileAttributeTypeID,
ProfileID = item.ProfileID,
Response = item.Response,
CreateDate = item.CreateDate,
TimeStamp = item.TimeStamp
});
HasAttributeId = true;
}
}
dc.Profiles.AddObject(profile);
}
dc.SaveChanges();
profileID1 = profile.ProfileID;
if (HasAttributeId)
{
var result = from pa in dc.ProfileAttributes.Include("ProfileAttributeType")
where pa.ProfileID == profileID1
select pa;
profile.Attributes = result.ToList();
foreach (ProfileAttribute item in profile.ProfileAttributes)
{
item.profileAttributeType = item.ProfileAttributeType;
}
}
}
return profileID1;
}
}
}
ItokModel.context.cs包含以下代码:
namespace Itok.Entities
{
public partial class ItokDataContext : ObjectContext
{
public const string ConnectionString = "name=ItokDataContext";
public const string ContainerName = "ItokDataContext";
#region Constructors
public ObjectSet<ProfileAttribute> ProfileAttributes
{
get { return _profileAttributes ?? (_profileAttributes = CreateObjectSet<ProfileAttribute>("ProfileAttributes")); }
}
private ObjectSet<ProfileAttribute> _profileAttributes;
}
}
Profile.cs包含以下代码:
namespace Itok.Entities
{
public partial class Profile
{
public List<ProfileAttribute> Attributes { get; set; }
public LevelOfExperienceType levelOfExperienceType { get; set; }
public Profile()
{
Attributes = new List<ProfileAttribute>();
}
}
}
这是StackTrace:
at System.RuntimeType.GetPropertyImpl(String name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers)
at System.Type.GetProperty(String name, BindingFlags bindingAttr)
at System.Linq.Expressions.Expression.PropertyOrField(Expression expression, String propertyOrFieldName)
at System.Data.Objects.Internal.EntityProxyFactory.CreateBaseGetter(Type declaringType, PropertyInfo propertyInfo)
at System.Data.Objects.Internal.PocoPropertyAccessorStrategy.GetNavigationPropertyValue(RelatedEnd relatedEnd)
at System.Data.Objects.Internal.EntityWrapper`1.GetNavigationPropertyValue(RelatedEnd relatedEnd)
at System.Data.Objects.EntityEntry.FixupFKValuesFromNonAddedReferences()
at System.Data.Objects.ObjectContext.AddSingleObject(EntitySet entitySet, IEntityWrapper wrappedEntity, String argumentName)
at System.Data.Objects.ObjectContext.AddObject(String entitySetName, Object entity)
at System.Data.Objects.ObjectSet`1.AddObject(TEntity entity)
at Itok.DataAccess.Repositories.ProfileRepository.SaveProfile(Profile profile) in C:\Users\S400\Documents\Visual Studio 2010\Projects\Itok\Itok\DataAccess\Repositories\ProfileRepository.cs:line 84
有没有人知道如何避免这种异常?
答案 0 :(得分:0)
我成功清除了异常。感谢所有人的好评和评论。
但对于仍然遇到困难并节省查找例外时间的人,您可能会发现以下步骤有助于更轻松地遵循和清除例外:
不要通过谷歌搜索过多的情况分散自己的注意力,这些可能无法准确应对你的情况(尽管其中很多都是指令性的)。事实上,最值得信赖的方式是依靠MSDN方向;
发生AmbiguousMatchException的情况包括:
2-1。类型包含两个具有相同名称但参数数量不同的索引属性。要解决歧义,请使用指定参数类型的GetProperty方法的重载。
2-2。派生类型使用new修饰符(Visual Basic中的Shadows)声明一个隐藏具有相同名称的继承属性的属性。要解决歧义,请包括BindingFlags.DeclaredOnly以限制搜索到未继承的成员。
最诚挚的问候 Babak Soltan