0
votes

I am in the process of using Entity Framework over a Visual FoxPro database (I haven't heard of it either). I've run into a problem creating the relationships using data annotations for the models.

I've simplified the classes:

public class Country
{
    [Key]
    public int Sid { get; set; }

    [ForeignKey("ObjectId")]
    public virtual ICollection<Heading> Headings { get; set; }

    ...
}

public class Heading
{
    [Key]
    public int Id { get; set; }

    public string ObjectId { get; set; }
}

Relationship: A Country has many Headings. (One to many)

Note that Country has an Id which is an int. And the ObjectId (foreign key) on Heading is a string.

I'm receiving the following exception:

One or more validation errors were detected during model generation:

Country_Headings_Source_Country_Headings_Target: : The types of all properties in the Dependent Role of a referential constraint must be the same as the corresponding property types in the Principal Role. The type of property 'Headingid' on entity 'Heading' does not match the type of property 'Sid' on entity 'Country' in the referential constraint 'Country_Headings'.

The issue is that the data types are different. Is there any way that I can have this relationship?

Thanks.

1
What's the primary key on Heading? It reads as though you have a foreign key to a field that's not a key - Antony Koch
@AntonyKoch The Heading's primary key is Id. The relationship is Country's Sid to Heading's ObjectId. I hope that made sense. - Mike Eason

1 Answers

1
votes

Edit This used to work with enums, may work with this?

public class Country
{
    [Key]
    public int Sid { get; set; }

    [ForeignKey("ObjectIdInt")]
    public virtual ICollection<Heading> Headings { get; set; }

    ...
}

public class Heading
{
    [Key]
    public int Id { get; set; }

    public string ObjectId { get; set; }

    public int ObjectIdInt 
    {
        get { return int.Parse(ObjectId); }
        set { ObjectId = value.ToString() }
    }
}