views:

359

answers:

3

Hey all,

I am trying to add a property to one of the generated data classes the Entity Framework has created for me. I have done the exact same thing on another generated class without a problem and for some reason it won't work on this one.

The only difference between the two generated objects is one is just a straight table mapping (the one that works) and the other inherits from another object (the one that doesn't work). In other words one table represents two entities and based on some criteria i've abstracted it up and created two inheriting entities...

<EntityType Name="Product" Abstract="true">
    <Key>
        <PropertyRef Name="Id" />
    </Key>
    <Property Name="Id" Type="String" Nullable="false" />
</EntityType>
<EntityType Name="Key" BaseType="MyEntities.Product">
    <Property Name="Status" Type="String" Nullable="true" />
    <Property Name="SerialNumberString" Type="String" Nullable="true" />
</EntityType>
<EntityType Name="License" BaseType="MyEntities.Product" >
    <Property Name="ProductCode" Type="String" Nullable="true" />
    <Property Name="Version" Type="String" Nullable="true" />
</EntityType>

Ive then added the property in follows...

public partial class Key
{
    public int? SerialNumber
    {
        get
        {
            int serialNumber;
            if (int.TryParse(SerialNumberString, out serialNumber))
            {
                return serialNumber;
            }

            return null;
        }

        set
        {
            SerialNumberString = value.ToString();
            ReportPropertyChanged("SerialNumber");
        }
    }
}

Now when ReportPropertyChanged gets called it throws this exception:

System.ArgumentException: The property 'SerialNumber' does not have a valid entity mapping on the entity object. For more information, see the Entity Framework documentation.

I have IDENTICAL code in another generated data class which does not throw, the only difference is the inheritance, whats going on??

A: 

I've never done this sort of thing before, but I notice in the mapping it says "SerialNumberString" and the property it's complaining about is "SerialNumber". Are they meant to be the same?

John Saunders
SerialNumberString is mapped to an nvarchar column which only contains numeric data, so my added SerialNumber property is wrapping that so i can use it as a number.
andrej351
Yeah, and I'm saying maybe that's what EF doesn't like. Rename "SerialNumber" to "SerialNumber2" and see if it starts complaining about the "SerialNumber2".
John Saunders
yeh it does, regardless of name
andrej351
A: 

Just a shot in the dark. Is "Key" a reserved word? If it were me, I'd change that entity name to something less related to the data structures.

For troubleshooting, does this error occur if you were to extend the base class "Product" instead of the inherited class?

itchi
The name "Key" isnt the problem as the entities have been working fine in this structure for a few weeks before trying to implement this.I tried adding the property to a partial extension of the Product class and it threw on the same line ('set' just had a call to ReportPropertyChanged as SerialNumberString doesnt exist in Product).
andrej351
+1  A: 

As far as the EF is concerned there is no SerialNumber property. The only property that it knows about it SerialNumberString.

So that is the SerialNumberString property that is changed, not the SerialNumber.

i.e. you want to do this: ReportPropertyChanged("SerialNumberString");

In fact that is redundant in your code because you are calling the Public SerialNumberString setter in your SerialNumber setter i.e:

set
{
    SerialNumberString = value.ToString();
    ReportPropertyChanged("SerialNumberString");
}

So SerialNumberString setter will call ReportPropertyChanged("SerialNumberString") anyway... so you should just remove that line altogether:

set
{
    SerialNumberString = value.ToString();
}

Hope this helps

Alex

BTW you should probably handle value.HasValue == false in the Setter too.

Alex James