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

  • SEARCH
  • Home
  • 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 6011703
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:17:53+00:00 2026-05-23T02:17:53+00:00

I have a basic question in MVC3. I am starting to write out Models

  • 0

I have a basic question in MVC3. I am starting to write out Models for my project. I understand Models are in a way tables in my relational database layer. How do I specify a class attribute (field in a table) as a FK. I know the [Key] qualifier for PKs. For example :

 public class Class1
{
    [Key]
    public int MyID { get; set; }
    public string myDes {get;set;}

}

In the above example MyId is a PK in Class1. How do I use this MyId in a another class and use it as FK.

EDIT: Is this correct?

public class Class2

{
[Key]
public int SecondID { get; set; }

[ForeignKey("MyId")]
public string MyId{ get; set; }

}

Class1 and Class2 are in two different .cs files.

  • 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-23T02:17:53+00:00Added an answer on May 23, 2026 at 2:17 am

    Store an instance of the associated record (or a list of associated records for one-to-many or many-to-many relationships).

    public MyOtherObject MyObject { get; set; }
    

    This may change based how you are persisting the data to the DB, but in general this is the OO way to do it.

    I do this using EF 4.1 Code First, and the resulting DB contains a Foreign Key into the associated table.


    Edit in response to your latest comment (regarding the ForeignKeyAttribute) so I can use the better code formatting in the answer:

    According to the MSDN entry for the ForeignKeyAttribute (http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.foreignkeyattribute(v=vs.103).aspx#Y198), the name you pass into the ForeignKeyAttribute is the name of the corresponding navigation property or foreign key property on the same class.

    For example:

    public class MyClass
    {
        // This is a "Navigation Property"
        public MyOtherClass ForeignObject { get; set; }
    
        // This is a "Foreign Key Property"
        [ForeignKey("ForeignObject")]
        public int ForeignObjectID { get; set; }
    }
    

    The ForeignKey attribute in the above code snippet identifies the ForeignObjectID property as a foreign key for the ForeignObject property. You will still need an instance of the foreign object in your class.

    You can also use the ForeignKey attribute on the navigation property, just pass in the name of the foreign key property to the attribute:

    public class MyClass
    {
        // This is a "Navigation Property"
        [ForeignKey("ForeignObjectID")]
        public MyOtherClass ForeignObject { get; set; }
    
        // This is a "Foreign Key Property"
        public int ForeignObjectID { get; set; }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a basic question about starting a frame-by-frame animation. When I call the
This is a basic question. I have the basic SL4/RIA project set up and
I am starting to implement an MTI solution and have a basic question. I
i have this basic question : is there a way to declare a group
I have a basic question: I am getting text data from a database and
I have a basic question for all of the math experts out there. If
I have a basic question, in the Django template language how can you tell
I have a basic question about assembly. Why do we bother doing arithmetic operations
I have really basic question. How can I get form id by input element
I have a basic PHP question, take the code below for example, let's say

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.