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.
.net entity-framework asp.net-core-2.0
add a comment |
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.
.net entity-framework asp.net-core-2.0
1
Please see this link: codeproject.com/Articles/206410/…
– Farhad Mortezapour
yesterday
Thanks for the link it helped me a lot
– Space
yesterday
add a comment |
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.
.net entity-framework asp.net-core-2.0
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
.net entity-framework asp.net-core-2.0
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
add a comment |
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
add a comment |
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.
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered yesterday
Harald Coppoolse
10.9k12958
10.9k12958
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
1
Please see this link: codeproject.com/Articles/206410/…
– Farhad Mortezapour
yesterday
Thanks for the link it helped me a lot
– Space
yesterday