4
I have the following class structure, unconventional but it’s in the mold that I need to solve my problem:
Tree:
public class Arvore
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[Required]
[StringLength(50)]
public string Descricao { get; set; }
public virtual ICollection<Galho_TipoA> Galhos_TipoA { get; set; }
public virtual ICollection<Galho_TipoB> Galhos_TipoB { get; set; }
}
Twig:
public class Galho
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[Required]
public int ArvoreId { get; set; }
[Required]
[StringLength(50)]
public string Descricao { get; set; }
[ForeignKey("ArvoreId")]
public Arvore Avore { get; set; }
}
public class Galho_TipoA : Galho { }
public class Galho_TipoB : Galho { }
When generating my schema I am getting the following:
CreateTable(
"dbo.Arvores",
c => new
{
Id = c.Int(nullable: false, identity: true),
Descricao = c.String(nullable: false, maxLength: 50),
})
.PrimaryKey(t => t.Id);
CreateTable(
"dbo.Galhos",
c => new
{
Id = c.Int(nullable: false, identity: true),
ArvoreId = c.Int(nullable: false),
Descricao = c.String(nullable: false, maxLength: 50),
Discriminator = c.String(nullable: false, maxLength: 128),
Arvore_Id = c.Int(),
Arvore_Id1 = c.Int(),
})
.PrimaryKey(t => t.Id)
.ForeignKey("dbo.Arvores", t => t.Arvore_Id)
.ForeignKey("dbo.Arvores", t => t.Arvore_Id1)
.ForeignKey("dbo.Arvores", t => t.ArvoreId, cascadeDelete: true)
.Index(t => t.ArvoreId)
.Index(t => t.Arvore_Id)
.Index(t => t.Arvore_Id1);
Note that various indexes are being generated for the properties of Arvore
who are of the same type:
.ForeignKey("dbo.Arvores", t => t.Arvore_Id)
.ForeignKey("dbo.Arvores", t => t.Arvore_Id1)
.ForeignKey("dbo.Arvores", t => t.ArvoreId, cascadeDelete: true)
.Index(t => t.ArvoreId)
.Index(t => t.Arvore_Id)
.Index(t => t.Arvore_Id1);
I created the guys Galho_TipoA
and Galho_TipoB
exactly to differentiate the records by field Discriminator
, but it didn’t work. Instead of pointing to ArvoreId
created others, one for each property in Arvore
.
How to resolve this issue to ArvoreId
and not need to generate others?
Or, what would be the correct way to structure this scheme?
Editing
The need for logic that the model needs to meet is as follows: Arvore
may have several Galhos
of various kinds.
So I thought of the guys as separate properties.
I don’t quite understand what the purpose is if I can put which I improve the answer or even put another answer! I found this interesting and the effect really worked with both sides
– Maria
It’s not easier to put the branch guy, so?
– Maria