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 7587073
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T19:34:59+00:00 2026-05-30T19:34:59+00:00

I have a class with a relationship to another table. public class MyClass {

  • 0

I have a class with a relationship to another table.

public class MyClass
{
    [Key]
    public Guid Id {get; set; }

    public virtual OtherClass OtherClass { get; set; }
}

I hook this up to a controller and create views for CRUD – all works fine.

In the DB a OtherClass_OtherClassId column is created, but this is not in the model.

How can I put a reference in this Id column during the controller’s Create method?

How can I force this relationship to be [Required] without having to create a brand new OtherClass each time?

  • 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-30T19:35:00+00:00Added an answer on May 30, 2026 at 7:35 pm

    Annotated class with some description:

    public class MyClass
    {
        // [Key] - Don't actually need this attribute
        // EF Code First has a number of conventions.
        // Columns called "Id" are assumed to be the Key.
        public Guid Id {get; set; }
    
        // This reference creates an 'Independent Association'.  The Database
        // foreign key is created by convention and hidden away in the code.
        [Required]
        public virtual OtherClass OtherClass { get; set; }
    
        // This setup explicitly declares the foreign key property.
        // Again, by convention, EF assumes that "FooId" will be the key for
        // a reference to object "Foo"
    
        // This will still be required and a cascade-on-delete property
        // like above - an int? would make the association optional.
        public int OtherClass2Id { get; set; }
    
        // Leave the navigation property as this - no [Required]
        public virtual OtherClass2 { get; set; }
    }
    

    So which is better? Independent associations or declaring the foriegn key?

    Independent associations match object programming closer. With OOP, one object doesn’t really care much about the Id of a member. ORM’s try to cover these relationships up, with varying degrees of success.

    Declaring the foreign key puts database concerns into your model, but there are scenarios where this makes dealing with EF much easier.

    Example – when updating an object with a required independent association, EF will want to have the entire object graph in place.

    public class MyClass
    {
        public int Id {get; set; }
        public string Name { get; set; }
        [Required]  // Note the required.  An optional won't have issues below.
        public virtual OtherClass OtherClass { get; set; }
    }
    
    var c = db.MyClasses.Find(1);
    c.Name = "Bruce Wayne";
    
    // Validation error on c.OtherClass.
    // EF expects required associations to be loaded.
    db.SaveChanges();  
    

    If all you want to do is update the name, you’ll either have to pull OtherClass from the database as well since it’s required for entity validation or attach a stubbed entity (assuming you know the id). If you explicitly declare foreign key, then you won’t run into this scenario.

    Now with foreign keys, you run into a different issue:

    public class MyClass
    {
        public Guid Id {get; set; }
        public string Name { get; set; }
    
        public int OtherClassId { get; set }
        public virtual OtherClass OtherClass { get; set; }
    }
    
    var c = db.MyClasses.Find(1);
    
    // Stepping through dubugger, here, c.OtherClassId = old id
    
    c.OtherClass = somethingElse;
    
    // c.OtherClassId = old id - Object and id not synced!
    
    db.SaveChanges();               
    
    // c.OtherClassId = new id, association persists correctly though.
    

    In summary –

    Independent associations

    • Good: Match OOP and POCO’s better
    • Bad: Often requires a full object graph, even if you’re only updating one or two properties. More EF headaches.

    Foreign Keys

    • Good: Easier to work with sometimes – less EF headaches.
    • Bad: Can be out of sync with their object
    • Bad: Database concerns in your POCO’s
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following class: public class Element { public Guid Id { get;
I have the following objects: public class User { int Id { get; set;
I have class with internal property: internal virtual StateEnum EnrolmentState { get { ..getter
I have following two entities public class User { [Key] public int Id {
I have a one way @OneToMany relationship between a Team and Player class. I
I have two scaffold-generated Models, student and class . They have a many-to-many relationship
I have: class MyClass extends MyClass2 implements Serializable { //... } In MyClass2 is
I have class A: public class ClassA<T> Class B derives from A: public class
I have a class as below: public class Node { public int NodeID {
I have an AUDIO class. This audio has a SOUND_A subclass and a SOUND_B

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.