EntityWrapper 混乱

EntityWrapper Confusion

WPF

Entity Framework 6.0

数据库优先,实体由TT文件生成。

我在使用 EntityWrapper 时遇到一些问题,找不到任何有用的信息。

我有一些实体,生成时如下所示:

//generated code
public partial class scm_SupplierDepot : IPartsEntity, INotifyPropertyChanged
{
    [...]
    public virtual dms_Address dms_Address { get; set; }
}

public partial class dms_Address : IPartsEntity, INotifyPropertyChanged
{
    //shortened for brevity 
    public System.Guid AddressId  { get; set; }
    public string StreetNumber  { get; set; }
    public string StreetName  { get; set; }
    public string ApartmentNumber  { get; set; }
    public string City  { get; set; }
    public string StateProvince  { get; set; }
    public string PostalCode  { get; set; }
    public string HouseName  { get; set; }
    public string Country  { get; set; }
    public string Address2  { get; set; }
    public string County  { get; set; }

    //INotifyPropertyChanged 
    [..]
}

我用接口稍微扩展了地址class:

public partial class dms_Address : IAddress {  } 

public interface IAddress
{
    String StreetNumber { get; set; }
    String StreetName { get; set; }
    String ApartmentNumber { get; set; }
    String Address2 { get; set; }
    String City { get; set; }
    String StateProvince { get; set; }
    String PostalCode { get; set; }
    String County { get; set; }
    String Country { get; set; }        
}

关于从 scm_SupplierDepot 实体获取 dms_Address 实体,我遇到了一些困惑和问题。在大多数情况下,我可以将 Depot.dms_Address 转换为 IAddress 并毫无问题地使用该实体。

但是当我尝试将此对象绑定到自定义控件时,该控件接收到的实际对象是 EntityWrapper< dms_Address > EntityWrapperWithoutRelationships< dms_Address >

我必须让我的控件的依赖项 属性 接受 object,而不是我希望的 IAddress。现在我无法使用该对象,因为它 不会将 转换为 IAddress。我什至无法将它转换为 EntityWrapper,因为我不知道要包含的正确命名空间。

    public static readonly DependencyProperty AddressProperty = DependencyProperty.Register("Address", typeof(object), typeof(AddressForm), new FrameworkPropertyMetadata(null, AddressChanged));
    public object Address 
    {
        get { return (object)GetValue(AddressProperty); }
        set { SetValue(AddressProperty, value); }
    }

有关我的自定义控件和此依赖项 属性 问题的更多信息,请参阅上一个问题:

问题:

我想出了如何使用反射来完成我需要的事情。

        Type objectType = Address.GetType();
        Type iAdd = objectType.GetInterface("IAddress");

        if (iAdd != null)
        {
            PropertyInfo info = objectType.GetProperty("StateProvince");
            if (info != null)
            {
                string currentProvince = info.GetValue(Address) as string;

                if (currentProvince != newValue)
                    info.SetValue(Address, newValue);
            }
        }

我仍然对为什么会看到这种行为感到困惑;如果有接口,为什么我投不出来?

Type iAdd = Address.GetType().GetInterface("IAddress"); //iAdd is not null,
IAddress IA = (Address as IAddress); //IA is null

.

最后我设法改变了我的代码,使所有这些代码都变得不必要 >.<