Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • mani0525 2 posts 72 karma points
    Apr 06, 2023 @ 08:32
    mani0525
    0

    Database link objects within objects C#

    I know it's seems like a very newbie question but it's still confuse me how you can link objects within objects:

    For example I have an objects of Book:

    name pages publication Date author and so on.

    But my question will be on the Author itself is an object so how will I be able to link author to this book inside the database I mean One Author can have a lot of books so it's not right to save to every book the full author object.

    Thanks for your help

    I tried to look for any answer on Stack Overflow, but it seems to be a concept relatively simple but not for me.

  • Lewis Smith 211 posts 620 karma points c-trib
    Apr 06, 2023 @ 09:21
    Lewis Smith
    100

    Hi Mani,

    So this seems more a general C# question, so my answer will be with that in mind, if that is not correct then let me know.

    So, you author could be as follows:

     public class Author
     {
         public int Id{get;set;}
         public string Name{get;set}
         ...
     }
    

    and your books could be:

    public class Book{
        public int Id{get;set;}
        public int AuthorId{get;set;}
    }
    

    The Book.AuthorId would be the same Id the Author has, so you could then use this Id to get the Authors details such as name etc.

    If i were to add this to an Umbraco site, or a general site I would probably use a code first approach, this allows you to have strongly typed classes (objects) within classes, so you when you get your Book, youll get your Author back as well. See here for some more details https://learn.microsoft.com/en-us/ef/ef6/modeling/code-first/workflows/new-database

    Lewis

  • mani0525 2 posts 72 karma points
    Apr 06, 2023 @ 12:25
    mani0525
    0

    Thank You so much Lewis Smith for giving suggestion to me

Please Sign in or register to post replies

Write your reply to:

Draft