title: Entity Framework defining the foreign key
body: <p>Is there a way to assign a foreign key to  the table using Entity Framework? I have following table specification using database-first (with an EDMX model file) approach: </p>
  
  <p><strong>Table 1:</strong>           </p>
  
  <pre><code>classDate   
  classDate_Id(pk)
  classDate_reference
  classDate_start
  class_Id
  </code></pre>
  
  <p><strong>Table 2:</strong>          </p>
  
  <pre><code>class_Id(pk)
  class_Name
  classs_description
  </code></pre>
  
  <p>It is not possible to specify the class_id as a foreign key at the time of table creation (classdate). The entity table (classdate) itself with class_Id as a column (normal) not as a foreign key. I want to specify this one as foreign key. so.. is it possible to specify as a foreign key.</p>
  
  <p>This is the code I found in <code>tsgentitesmodel.designer.cs</code>. Do I need to change here? </p>
  
  <pre><code>  public partial class tsgEntities : ObjectContext
    {
      /// &lt;summary&gt;
      /// No Metadata Documentation available.
      /// &lt;/summary&gt;
      public ObjectSet&lt;classdate&gt; classdates
      {
          get
          {
              if ((_classdates == null))
              {
                  _classdates = base.CreateObjectSet&lt;classdate&gt;("classdates");
              }
              return _classdates;
          }
      }
      private ObjectSet&lt;classdate&gt; _classdates;
  
      /// &lt;summary&gt;
      /// No Metadata Documentation available.
      /// &lt;/summary&gt;
      public ObjectSet&lt;@class&gt; classes
      {
          get
          {
              if ((_classes == null))
              {
                  _classes = base.CreateObjectSet&lt;@class&gt;("classes");
              }
              return _classes;
          }
      }
      private ObjectSet&lt;@class&gt; _classes;
   }
  
       /// &lt;summary&gt;
      /// Deprecated Method for adding a new object to the classdates EntitySet. Consider using the .Add method of the associated ObjectSet&amp;lt;T&amp;gt; property instead.
      /// &lt;/summary&gt;
      public void AddToclassdates(classdate classdate)
      {
          base.AddObject("classdates", classdate);
      }
  
      /// &lt;summary&gt;
      /// Deprecated Method for adding a new object to the classes EntitySet. Consider using the .Add method of the associated ObjectSet&amp;lt;T&amp;gt; property instead.
      /// &lt;/summary&gt;
      public void AddToclasses(@class @class)
      {
          base.AddObject("classes", @class);
      }
  </code></pre>
  
  <p>And I have found the below code and do I need to change here </p>
  
  <pre><code> public partial class classdate : EntityObject
  {
      #region Factory Method
  
      /// &lt;summary&gt;
      /// Create a new classdate object.
      /// &lt;/summary&gt;
      /// &lt;param name="classDate_End"&gt;Initial value of the classDate_End property.&lt;/param&gt;
      /// &lt;param name="classDate_Id"&gt;Initial value of the classDate_Id property.&lt;/param&gt;
      /// &lt;param name="classDate_Reference"&gt;Initial value of the classDate_Reference property.&lt;/param&gt;
      /// &lt;param name="classDate_Start"&gt;Initial value of the classDate_Start property.&lt;/param&gt;
      /// &lt;param name="class_Id"&gt;Initial value of the class_Id property.&lt;/param&gt;
      public static classdate Createclassdate(global::System.String classDate_End, global::System.Int32 classDate_Id, global::System.String classDate_Reference, global::System.String classDate_Start, global::System.Int32 class_Id)
      {
          classdate classdate = new classdate();
          classdate.classDate_End = classDate_End;
          classdate.classDate_Id = classDate_Id;
          classdate.classDate_Reference = classDate_Reference;
          classdate.classDate_Start = classDate_Start;
          classdate.class_Id = class_Id;
          return classdate;
      }
  
      [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)]
      [DataMemberAttribute()]
      public global::System.Int32 class_Id
      {
          get
          {
              return _class_Id;
          }
          set
          {
              Onclass_IdChanging(value);
              ReportPropertyChanging("class_Id");
              _class_Id = StructuralObject.SetValidValue(value);
              ReportPropertyChanged("class_Id");
              Onclass_IdChanged();
          }
      }
      private global::System.Int32 _class_Id;
      partial void Onclass_IdChanging(global::System.Int32 value);
      partial void Onclass_IdChanged();
  
    // same like this for all columns in calss data table 
  </code></pre>
  
  <p>Below is the code for class entity:</p>
  
  <pre><code>  /// &lt;summary&gt;
  /// No Metadata Documentation available.
  /// &lt;/summary&gt;
  [EdmEntityTypeAttribute(NamespaceName="tsgEntities", Name="class")]
  [Serializable()]
  [DataContractAttribute(IsReference=true)]
  public partial class @class : EntityObject
  {
      #region Factory Method
  
      /// &lt;summary&gt;
      /// Create a new class object.
      /// &lt;/summary&gt;
      /// &lt;param name="class_Description"&gt;Initial value of the class_Description property.&lt;/param&gt;
      /// &lt;param name="class_Id"&gt;Initial value of the class_Id property.&lt;/param&gt;
      /// &lt;param name="class_Name"&gt;Initial value of the class_Name property.&lt;/param&gt;
      /// &lt;param name="class_NumSessions"&gt;Initial value of the class_NumSessions property.&lt;/param&gt;
      /// &lt;param name="class_Reference"&gt;Initial value of the class_Reference property.&lt;/param&gt;
      public static @class Createclass(global::System.String class_Description, global::System.Int32 class_Id, global::System.String class_Name, global::System.String class_NumSessions, global::System.String class_Reference)
      {
          @class @class = new @class();
          @class.class_Description = class_Description;
          @class.class_Id = class_Id;
          @class.class_Name = class_Name;
          @class.class_NumSessions = class_NumSessions;
          @class.class_Reference = class_Reference;
          return @class;
      }
  
      #endregion
      #region Primitive Properties
  
      /// &lt;summary&gt;
      /// No Metadata Documentation available.
      /// &lt;/summary&gt;
      [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)]
      [DataMemberAttribute()]
      public global::System.String class_Description
      {
          get
          {
              return _class_Description;
          }
          set
          {
              Onclass_DescriptionChanging(value);
              ReportPropertyChanging("class_Description");
              _class_Description = StructuralObject.SetValidValue(value, false);
              ReportPropertyChanged("class_Description");
              Onclass_DescriptionChanged();
          }
      }
      private global::System.String _class_Description;
      partial void Onclass_DescriptionChanging(global::System.String value);
      partial void Onclass_DescriptionChanged();
  
  
      //same like this for all coulmns 
  </code></pre>