locked
defining foreign key relationship RRS feed

  • Question

  • User364607740 posted

    Lets say there are two model classes,

    public class ClassA
    {
    	public int AId { get; set; }
    	...
    }
    
    public class ClassB
    {
    	public int BId { get; set; }
    	...
    }

    If I have to show foreign key relationship from ClassA to ClassB, there are two approaches,

    public class ClassB
    {
    	public int BId { get; set; }
    	...
    	[ForeignKey("AId")]
            public ClassA ClassAs { get; set; } 
            public int AId { get; set; } 
    }
    or
    public class ClassB
    {
    	public int BId { get; set; }
    	...
    	[ForeignKey("AId")]
    	public ICollection<ClassA> ClassAs { get; set; }
    }

    What is the difference in these two types of foreign key relationship?

    Friday, August 21, 2020 8:49 AM

Answers

  • User1686398519 posted

    Hi scala_1988,

    1. The [ForeignKey] attribute is used to override the default convention of foreign keys. It allows us to specify foreign key attributes in subordinate entities whose names do not match the primary key attributes of the principal entity.
    2. The two writing methods you mentioned are just two different methods of setting [ForeignKey].
    3. "ICollection<ClassA> ClassAs" is a collection navigation property that contains references to many related entities. "ClassA ClassAs" is a reference navigation attribute that contains a reference to a single related entity.

    Best Regards,

    YihuiSun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, August 25, 2020 7:56 AM

All replies

  • User475983607 posted

    Navigation property syntax is openly covered in the Entity Framework fundamentals.  Please read the docs for the version of entity framework you are using rather than posting the same information on the forums.

    EF Core

    https://docs.microsoft.com/en-us/ef/core/modeling/relationships?tabs=fluent-api%2Cfluent-api-simple-key%2Csimple-key

    EF 6

    https://docs.microsoft.com/en-us/ef/ef6/fundamentals/relationships

    Friday, August 21, 2020 11:21 AM
  • User1686398519 posted

    Hi scala_1988,

    1. The [ForeignKey] attribute is used to override the default convention of foreign keys. It allows us to specify foreign key attributes in subordinate entities whose names do not match the primary key attributes of the principal entity.
    2. The two writing methods you mentioned are just two different methods of setting [ForeignKey].
    3. "ICollection<ClassA> ClassAs" is a collection navigation property that contains references to many related entities. "ClassA ClassAs" is a reference navigation attribute that contains a reference to a single related entity.

    Best Regards,

    YihuiSun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, August 25, 2020 7:56 AM
  • User364607740 posted

    Thank You for explaining the topics. Thank you very much...

    Friday, August 28, 2020 9:11 AM