2

Basically put, I have an "Identity" table that has an ID and username, I have another table that has entries "owned" by a person, for that reason, I need to have a FK that links to the Identity table.

For example: Class "Identity" - int ID, string username.

I was just wondering which of the following is best:

  1. Class "test" - int ID, string data, Identity identity

  2. Class "test" - int ID, string data, int identity_id - with an annotation defining as foreign key.

I personally use the first, and seen that EF basically does the second behind the scene, but I was just wondering what the advantages/disadvantages are and what is best?

Ladislav Mrnka
  • 349,807
  • 56
  • 643
  • 654
Wil
  • 9,259
  • 11
  • 47
  • 80

1 Answers1

3

From the object oriented perspective the first is better because you have reference to the related object and it is what you expect when working with objects. In database this is performed by foreign key which defines relation between two records.

Entity framework offers both correct object oriented approach (the first one) and the approach where you include foreign key property in the entity as well. The reason is that exposing FK property will make some operations much more easier. Most common solution for the second approach is:

public class Test
{
    public int Id { get; set; }
    public string Data { get; set; }
    [ForeignKey("Identity")]
    public int IdentityId { get; set; }
    public Identity Identity { get; set; }
}

You have both access to FK property and the entity. Those approaches have names: Independent association (the first one) and Foreign key association (the second one). There are more differences between them - I described them in another answer.

Community
  • 1
  • 1
Ladislav Mrnka
  • 349,807
  • 56
  • 643
  • 654
  • Just what I wanted to hear... Going to use the first one then! +1 to this and the other answer you linked to! – Wil May 12 '11 at 14:35
  • Just wondering quickly if I had a very long chain of FKs, could this cause a performance problem, e.g. does it load the "entire chain" at first or does it only get the additional objects once they are queried? – Wil May 12 '11 at 19:12
  • First query will load only relations which you explicitly define by `Include` extension method. If you use lazy loading (navigation properties must be `virtual`) then navigation properties are loaded once you first access them. – Ladislav Mrnka May 12 '11 at 19:23
  • I am reading over many of my EF questions as I think I have asked too many! Sorry to bother you again, but, I think I finally get what you were trying to explain to me - In the end, I went for Virtual Identity identity, and when I want to do a compare/similar, I do something along the lines of "x.id == identity.id"... I was just wondering, if I was to implement both (which I never understood at the time - but, after rereading & an Eureka moment, I think I do), does this basically mean that I save a database query, But, I still get the benefit of lazy loading/access to properties when needed? – Wil Oct 30 '11 at 08:11
  • Yes that is correct assumption. Once you have FK directly in the entity you can save query in some cases. – Ladislav Mrnka Oct 30 '11 at 21:09
  • Thanks... Can't give another +1, but, you have made a lot of sense now - I have also read your article on your website (linked from the link in this answer).... - I just wish I understood this better at the time and designed my database accordingly, but, I feel it is too late now - I could of really benefited from this :( I will learn for the future! Many thanks again - you have answered so many questions/helped me a lot - I am going to see if I can set a bounty or something to give you some extra rep. – Wil Oct 30 '11 at 21:34
  • ... Ok, can award the bounty in 23 hours! – Wil Oct 30 '11 at 21:36