Entity framework data annotation of foreign key without a navigation property

c# data-annotations entity-framework entity-framework-6 foreign-keys

Question

I have the definition for the following table:

public partial class ClientClassification
{
    [Key, Column(Order = 0)]
    public long ClientId { get; set; }

    [Key, Column(Order = 1)]
    public ClientClassificationEnum ClientClassificationType { get; set; }

    [ForeignKey("ClientId")]
    public virtual Client Client { get; set; }
}

A foreign key into the Client table is ClientId. This relationship is one to many (a client can have many classifications). Is there a way to declare this foreign key relationship without including the Client navigation property? Hence, something like:

public partial class ClientClassification
{
    [Key, Column(Order = 0)]
    [ForeignKey("Client.ClientId")]
    public long ClientId { get; set; }

    [Key, Column(Order = 1)]
    public ClientClassificationEnum ClientClassificationType { get; set; }
}
1
1
2/5/2015 8:53:02 PM

Popular Answer

I would use a migration script to construct the relationship on the dtaabase because there won't be a relationship between "classes" but you still want to accomplish that (and no annotations on the class).

You could then declare a protected Client attribute (in this way, you would be specifying the Client navigation property but it wouldn't be accessible for anybody other than inherited classes) if you would still like to have the annotation on the class (for a reason that I can't envision right now).

0
4/30/2014 12:50:56 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow