Reputation: 91
I am working with an existing database where no foreign keys are defined. I can't change the database but would like to define relationships in my entity model. For example, the People table has all the names of the people but the Coaches table only has a reference to the PeopleId. I would like to define that relationship in my Coaches entity object.
Upvotes: 0
Views: 203
Reputation: 91
I turns out that with more testing it doesn't seem to matter that the database has not defined the foreign key. I'm still able to bring in the associated tables info. Additional testing will be needed because referential integrity is not enforced by the database. However with this table definition and model definition, my person data is being brought in.
[Key]
public int CoachID { get; set; }
public Nullable<int> CompanyID { get; set; }
public Nullable<int> SeasonID { get; set; }
public int PeopleID { get; set; }
public Nullable<int> PlayerID { get; set; }
public string ShirtSize { get; set; }
public string CoachPhone { get; set; }
public Nullable<System.DateTime> CreatedDate { get; set; }
public string CreatedUser { get; set; }
public virtual Person Person { get; set; }
modelBuilder
.Entity<Coach>()
.ToTable("Coaches")
.HasRequired(p => p.Person)
Upvotes: 2