Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6347479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T21:13:24+00:00 2026-05-24T21:13:24+00:00

Is there a way to assign a foreign key to the table using Entity

  • 0

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:

Table 1:

classDate   
classDate_Id(pk)
classDate_reference
classDate_start
class_Id

Table 2:

class_Id(pk)
class_Name
classs_description

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.

This is the code I found in tsgentitesmodel.designer.cs. Do I need to change here?

  public partial class tsgEntities : ObjectContext
  {
    /// <summary>
    /// No Metadata Documentation available.
    /// </summary>
    public ObjectSet<classdate> classdates
    {
        get
        {
            if ((_classdates == null))
            {
                _classdates = base.CreateObjectSet<classdate>("classdates");
            }
            return _classdates;
        }
    }
    private ObjectSet<classdate> _classdates;

    /// <summary>
    /// No Metadata Documentation available.
    /// </summary>
    public ObjectSet<@class> classes
    {
        get
        {
            if ((_classes == null))
            {
                _classes = base.CreateObjectSet<@class>("classes");
            }
            return _classes;
        }
    }
    private ObjectSet<@class> _classes;
 }

     /// <summary>
    /// Deprecated Method for adding a new object to the classdates EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
    /// </summary>
    public void AddToclassdates(classdate classdate)
    {
        base.AddObject("classdates", classdate);
    }

    /// <summary>
    /// Deprecated Method for adding a new object to the classes EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
    /// </summary>
    public void AddToclasses(@class @class)
    {
        base.AddObject("classes", @class);
    }

And I have found the below code and do I need to change here

 public partial class classdate : EntityObject
{
    #region Factory Method

    /// <summary>
    /// Create a new classdate object.
    /// </summary>
    /// <param name="classDate_End">Initial value of the classDate_End property.</param>
    /// <param name="classDate_Id">Initial value of the classDate_Id property.</param>
    /// <param name="classDate_Reference">Initial value of the classDate_Reference property.</param>
    /// <param name="classDate_Start">Initial value of the classDate_Start property.</param>
    /// <param name="class_Id">Initial value of the class_Id property.</param>
    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 

Below is the code for class entity:

  /// <summary>
/// No Metadata Documentation available.
/// </summary>
[EdmEntityTypeAttribute(NamespaceName="tsgEntities", Name="class")]
[Serializable()]
[DataContractAttribute(IsReference=true)]
public partial class @class : EntityObject
{
    #region Factory Method

    /// <summary>
    /// Create a new class object.
    /// </summary>
    /// <param name="class_Description">Initial value of the class_Description property.</param>
    /// <param name="class_Id">Initial value of the class_Id property.</param>
    /// <param name="class_Name">Initial value of the class_Name property.</param>
    /// <param name="class_NumSessions">Initial value of the class_NumSessions property.</param>
    /// <param name="class_Reference">Initial value of the class_Reference property.</param>
    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

    /// <summary>
    /// No Metadata Documentation available.
    /// </summary>
    [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 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T21:13:24+00:00Added an answer on May 24, 2026 at 9:13 pm

    If you want to create a relationship between Table2 and Table1 on class_Id, that column needs to be the primary key in Table1 – or you need to put a UNIQUE INDEX on it:

    CREATE UNIQUE INDEX UIX01_ClassDates ON dbo.ClassDates(class_Id)
    

    And then you should be able to create the foreign key relationship between your two tables:

    ALTER TABLE dbo.Classes
    ADD CONSTRAINT fk_classes_classdates
    FOREIGN KEY (Class_Id) REFERENCES dbo.ClassDates(class_Id)
    

    Now, once you’ve done that, refresh your EDMX model file and that relationship ought to show up automagically, now! (or you can manually add an Association between your two classes to make it show up)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to assign tasks to developers using TODO: statements in Mylyn
Is there a way to assign a process to a core (or more than
Is there a single-expression way to assign a scalar to all elements of a
Is there an easy way in Visual Studio to assign default usings when i
Is there way in next piece of code to only get the first record?
Is there a way to assign a font to a list that is added
We wanted to assign/add an item to a Dictionary using the key index, like
Photographers have_many clients. Clients have_many events. Is there a better way to assign @events
Is there any way to assign more memory for a Memo or RichEdit (if
Is there a way to assign a multiline string value in Delphi without having

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.