user547794
user547794

Reputation: 14521

Creating Navigation Property without foreign key?

I am trying to create a navigation property for my data model using an existing database. The data has a shared key (like a foreign key) however it is not specified that way in the database. Here is what it looks like:

 [Table("Alpha")]
    public class Alpha
    {
        public int AlphaID { get; set; }

        public int AlphaGroupID { get; set; }

        public int? ParentAlphaID { get; set; }

        public int? CodeID { get; set; }

        public int? BracketCodeID { get; set; }

        public string Title { get; set; }
    }





[Table("AlphaGroup")]
    public class AlphaGroup
    {
        [Column("AlphaGroupID")]
        public int AlphaGroupID { get; set; }

        public string Title { get; set; }

        public string TitleAddon { get; set; }

    }

AlphaGroupID needs to be a navigation property to AlphaGroupID but is not a foreign key. Is there a way to accomplish this?

Upvotes: 0

Views: 287

Answers (1)

Rusty
Rusty

Reputation: 1303

one way can be using the icollection in the parent class like in your example:

public virtual ICollection<AlphaGroup> AlphaGroup { get; set; }

Upvotes: 1

Related Questions