Make a table id its own foreign key entity framework











up vote
0
down vote

favorite












I wanted to know if it was possible to create a table with an id, a label and a foreign key referring to the id of the table itself.
Below is the example of what I would like to do but that does not work because the public virtual RubricFo can not be called by itself.



public class RubricFO
{
[Key, Required]
public int IdRubricFO { get; set; }

[MaxLength(250)]
public string LabelRubricFO { get; set; }

public bool IsActif { get; set; }

public int RankDisplay { get; set; }

[ForeignKey("IdRubricFO")]
public int IdRubricFO_Fk { get; set; }
public virtual RubricFO RubricFO { get; set; }

public int IdStructure { get; set; }

[ForeignKey("IdStructure")]
public virtual Structures Structures { get; set; }
}


I do not know if I am clear enough if you need additional info do not hesitate to ask.










share|improve this question


















  • 1




    Please see this link: codeproject.com/Articles/206410/…
    – Farhad Mortezapour
    yesterday












  • Thanks for the link it helped me a lot
    – Space
    yesterday















up vote
0
down vote

favorite












I wanted to know if it was possible to create a table with an id, a label and a foreign key referring to the id of the table itself.
Below is the example of what I would like to do but that does not work because the public virtual RubricFo can not be called by itself.



public class RubricFO
{
[Key, Required]
public int IdRubricFO { get; set; }

[MaxLength(250)]
public string LabelRubricFO { get; set; }

public bool IsActif { get; set; }

public int RankDisplay { get; set; }

[ForeignKey("IdRubricFO")]
public int IdRubricFO_Fk { get; set; }
public virtual RubricFO RubricFO { get; set; }

public int IdStructure { get; set; }

[ForeignKey("IdStructure")]
public virtual Structures Structures { get; set; }
}


I do not know if I am clear enough if you need additional info do not hesitate to ask.










share|improve this question


















  • 1




    Please see this link: codeproject.com/Articles/206410/…
    – Farhad Mortezapour
    yesterday












  • Thanks for the link it helped me a lot
    – Space
    yesterday













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I wanted to know if it was possible to create a table with an id, a label and a foreign key referring to the id of the table itself.
Below is the example of what I would like to do but that does not work because the public virtual RubricFo can not be called by itself.



public class RubricFO
{
[Key, Required]
public int IdRubricFO { get; set; }

[MaxLength(250)]
public string LabelRubricFO { get; set; }

public bool IsActif { get; set; }

public int RankDisplay { get; set; }

[ForeignKey("IdRubricFO")]
public int IdRubricFO_Fk { get; set; }
public virtual RubricFO RubricFO { get; set; }

public int IdStructure { get; set; }

[ForeignKey("IdStructure")]
public virtual Structures Structures { get; set; }
}


I do not know if I am clear enough if you need additional info do not hesitate to ask.










share|improve this question













I wanted to know if it was possible to create a table with an id, a label and a foreign key referring to the id of the table itself.
Below is the example of what I would like to do but that does not work because the public virtual RubricFo can not be called by itself.



public class RubricFO
{
[Key, Required]
public int IdRubricFO { get; set; }

[MaxLength(250)]
public string LabelRubricFO { get; set; }

public bool IsActif { get; set; }

public int RankDisplay { get; set; }

[ForeignKey("IdRubricFO")]
public int IdRubricFO_Fk { get; set; }
public virtual RubricFO RubricFO { get; set; }

public int IdStructure { get; set; }

[ForeignKey("IdStructure")]
public virtual Structures Structures { get; set; }
}


I do not know if I am clear enough if you need additional info do not hesitate to ask.







.net entity-framework asp.net-core-2.0






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked yesterday









Space

525




525








  • 1




    Please see this link: codeproject.com/Articles/206410/…
    – Farhad Mortezapour
    yesterday












  • Thanks for the link it helped me a lot
    – Space
    yesterday














  • 1




    Please see this link: codeproject.com/Articles/206410/…
    – Farhad Mortezapour
    yesterday












  • Thanks for the link it helped me a lot
    – Space
    yesterday








1




1




Please see this link: codeproject.com/Articles/206410/…
– Farhad Mortezapour
yesterday






Please see this link: codeproject.com/Articles/206410/…
– Farhad Mortezapour
yesterday














Thanks for the link it helped me a lot
– Space
yesterday




Thanks for the link it helped me a lot
– Space
yesterday












1 Answer
1






active

oldest

votes

















up vote
2
down vote













Yes, it is possible. You see this if you want a Tree structure, where every node of the Tree has zero or more SubNodes, no ParentNode if it is a top node, or one ParentNode if it is a SubNode.



class Node
{
public int Id {get; set;}

// every Node has zero or more subNodes:
public virtual ICollection<Node> SubNodes {get; set;}

// every Node is the subNode of zero or one ParentNode, using foreign key
public int? ParentId {get; set;} // null if it is a Top Node
public virtual Node Parent {get; set;}
}


I'm pretty sure, that this is enough information for entity framework to understand the relations.



If not, you can use fluent API in your DbContext to inform entity framework about the model



protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// build table Nodes:
modelBuilder.Entity<Node>()
.HasKey(node => node.Id) // not needed, I followed the conventions
.HasOptional(node => node.Parent) // every node table has an optional Parent
.WithMany(node => node.SubNodes) // every Parent Node has zero or more SubNodes
.HasForeignKey(node => node.ParentId); // the foreign key to the parent node


Nice exercise: try int ParentId instead of int?, a zero value could mean there is no parent.






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53409120%2fmake-a-table-id-its-own-foreign-key-entity-framework%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote













    Yes, it is possible. You see this if you want a Tree structure, where every node of the Tree has zero or more SubNodes, no ParentNode if it is a top node, or one ParentNode if it is a SubNode.



    class Node
    {
    public int Id {get; set;}

    // every Node has zero or more subNodes:
    public virtual ICollection<Node> SubNodes {get; set;}

    // every Node is the subNode of zero or one ParentNode, using foreign key
    public int? ParentId {get; set;} // null if it is a Top Node
    public virtual Node Parent {get; set;}
    }


    I'm pretty sure, that this is enough information for entity framework to understand the relations.



    If not, you can use fluent API in your DbContext to inform entity framework about the model



    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
    // build table Nodes:
    modelBuilder.Entity<Node>()
    .HasKey(node => node.Id) // not needed, I followed the conventions
    .HasOptional(node => node.Parent) // every node table has an optional Parent
    .WithMany(node => node.SubNodes) // every Parent Node has zero or more SubNodes
    .HasForeignKey(node => node.ParentId); // the foreign key to the parent node


    Nice exercise: try int ParentId instead of int?, a zero value could mean there is no parent.






    share|improve this answer

























      up vote
      2
      down vote













      Yes, it is possible. You see this if you want a Tree structure, where every node of the Tree has zero or more SubNodes, no ParentNode if it is a top node, or one ParentNode if it is a SubNode.



      class Node
      {
      public int Id {get; set;}

      // every Node has zero or more subNodes:
      public virtual ICollection<Node> SubNodes {get; set;}

      // every Node is the subNode of zero or one ParentNode, using foreign key
      public int? ParentId {get; set;} // null if it is a Top Node
      public virtual Node Parent {get; set;}
      }


      I'm pretty sure, that this is enough information for entity framework to understand the relations.



      If not, you can use fluent API in your DbContext to inform entity framework about the model



      protected override void OnModelCreating(DbModelBuilder modelBuilder)
      {
      // build table Nodes:
      modelBuilder.Entity<Node>()
      .HasKey(node => node.Id) // not needed, I followed the conventions
      .HasOptional(node => node.Parent) // every node table has an optional Parent
      .WithMany(node => node.SubNodes) // every Parent Node has zero or more SubNodes
      .HasForeignKey(node => node.ParentId); // the foreign key to the parent node


      Nice exercise: try int ParentId instead of int?, a zero value could mean there is no parent.






      share|improve this answer























        up vote
        2
        down vote










        up vote
        2
        down vote









        Yes, it is possible. You see this if you want a Tree structure, where every node of the Tree has zero or more SubNodes, no ParentNode if it is a top node, or one ParentNode if it is a SubNode.



        class Node
        {
        public int Id {get; set;}

        // every Node has zero or more subNodes:
        public virtual ICollection<Node> SubNodes {get; set;}

        // every Node is the subNode of zero or one ParentNode, using foreign key
        public int? ParentId {get; set;} // null if it is a Top Node
        public virtual Node Parent {get; set;}
        }


        I'm pretty sure, that this is enough information for entity framework to understand the relations.



        If not, you can use fluent API in your DbContext to inform entity framework about the model



        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
        // build table Nodes:
        modelBuilder.Entity<Node>()
        .HasKey(node => node.Id) // not needed, I followed the conventions
        .HasOptional(node => node.Parent) // every node table has an optional Parent
        .WithMany(node => node.SubNodes) // every Parent Node has zero or more SubNodes
        .HasForeignKey(node => node.ParentId); // the foreign key to the parent node


        Nice exercise: try int ParentId instead of int?, a zero value could mean there is no parent.






        share|improve this answer












        Yes, it is possible. You see this if you want a Tree structure, where every node of the Tree has zero or more SubNodes, no ParentNode if it is a top node, or one ParentNode if it is a SubNode.



        class Node
        {
        public int Id {get; set;}

        // every Node has zero or more subNodes:
        public virtual ICollection<Node> SubNodes {get; set;}

        // every Node is the subNode of zero or one ParentNode, using foreign key
        public int? ParentId {get; set;} // null if it is a Top Node
        public virtual Node Parent {get; set;}
        }


        I'm pretty sure, that this is enough information for entity framework to understand the relations.



        If not, you can use fluent API in your DbContext to inform entity framework about the model



        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
        // build table Nodes:
        modelBuilder.Entity<Node>()
        .HasKey(node => node.Id) // not needed, I followed the conventions
        .HasOptional(node => node.Parent) // every node table has an optional Parent
        .WithMany(node => node.SubNodes) // every Parent Node has zero or more SubNodes
        .HasForeignKey(node => node.ParentId); // the foreign key to the parent node


        Nice exercise: try int ParentId instead of int?, a zero value could mean there is no parent.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered yesterday









        Harald Coppoolse

        10.9k12958




        10.9k12958






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53409120%2fmake-a-table-id-its-own-foreign-key-entity-framework%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

            Calculate evaluation metrics using cross_val_predict sklearn

            Insert data from modal to MySQL (multiple modal on website)