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 following table definition:

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

ClientId is a foreign key into the Client table. This is a many to 1 relationship (a client can have many classifications). Is there any way I can omit the Client navigation property but still declare this foreign key relationship? So 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

Since there will be no relationship between "classes" and still you want to create the relationship on the dtaabase, I would be using a migration script to do it (and no annotations on the class).

If you would still like to have the annotation on the class (for a reason that I can't imagine right now) you could then declare a protected Client attribute (this way you would be declaring the Client navigation property but it wouldn't be accessible for anyone but inherited classes).

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