0

WPF

Entity Framework 6.0

Database first, entities are generated by TT file.

I'm having some problems with EntityWrapper, and can't find any useful information about it.

I have some entities, that when generated looks like this:

//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 
    [..]
}

I extend the address class slightly with an interface:

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; }        
}

I am having some confusion and issues around getting the dms_Address entity from the scm_SupplierDepot entity. In most cases I can cast the Depot.dms_Address as IAddress and work with the entity with no issues.

But when I try binding this object to a Custom Control, the actual object that the control receives is a EntityWrapper< dms_Address > or EntityWrapperWithoutRelationships< dms_Address >

I had to make my control's dependency property accept an object, rather than an IAddress as I would prefer. Now I cannot work with the object as it will not cast to IAddress. I can't even cast it to EntityWrapper as I can't figure out the correct namespace to include.

    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); }
    }

More information about my Custom Control and this Dependency Property issue can be read in a previous question: WPF Custom Control: DependencyProperty never Set (on only 1 of many properties)

Questions:

  • Can anyone explain to me what is going on here?
  • I don't understand where this wrapper is coming from. How can I make it go away?
  • How can I get the control to receive the IAddress instead of the wrapper?
  • Or how can I cast the EntityWrapper object to IAddress so I can access the properties in code? (oddly enough the template bindings work fine)
Community
  • 1
  • 1
Shaboboo
  • 1,347
  • 1
  • 16
  • 35

1 Answers1

0

I figured out how to do what I needed by using Reflection.

        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);
            }
        }

I am still stumped on why I'm seeing this behaviour; if it has the interface, why can't I cast it?

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

.

In the end I managed to switch my code around to make all of this code unnecessary >.<

Shaboboo
  • 1,347
  • 1
  • 16
  • 35