Well I’ll propose to that model then:
Database - Layout
Classes Models and Fluent Mapping
public partial class Acao
{
public Acao()
{
this.FluxoPassoAcaos = new List<FluxoPassoAcao>();
}
public int ID_ACAO { get; set; }
public string DESCRICAO { get; set; }
public virtual ICollection<FluxoPassoAcao> FluxoPassoAcaos { get; set; }
}
public class AcaoMap : EntityTypeConfiguration<Acao>
{
public AcaoMap()
{
this.HasKey(t => t.ID_ACAO);
this.Property(t => t.DESCRICAO).HasMaxLength(50);
this.ToTable("Acao");
this.Property(t => t.ID_ACAO).HasColumnName("ID_ACAO");
this.Property(t => t.DESCRICAO).HasColumnName("DESCRICAO");
}
}
public partial class Fluxo
{
public Fluxo()
{
this.FluxoPassoAcaos = new List<FluxoPassoAcao>();
}
public int ID_FLUXO { get; set; }
public string DESCRICAO { get; set; }
public virtual ICollection<FluxoPassoAcao> FluxoPassoAcaos { get; set; }
}
public class FluxoMap : EntityTypeConfiguration<Fluxo>
{
public FluxoMap()
{
this.HasKey(t => t.ID_FLUXO);
this.Property(t => t.DESCRICAO).IsRequired().HasMaxLength(50);
this.ToTable("Fluxo");
this.Property(t => t.ID_FLUXO).HasColumnName("ID_FLUXO");
this.Property(t => t.DESCRICAO).HasColumnName("DESCRICAO");
}
}
public partial class Passo
{
public Passo()
{
this.FluxoPassoAcaos = new List<FluxoPassoAcao>();
}
public int ID_PASSO { get; set; }
public string DESCRICAO { get; set; }
public virtual ICollection<FluxoPassoAcao> FluxoPassoAcaos { get; set; }
}
public class PassoMap : EntityTypeConfiguration<Passo>
{
public PassoMap()
{
this.HasKey(t => t.ID_PASSO);
this.Property(t => t.DESCRICAO).HasMaxLength(50);
this.ToTable("Passo");
this.Property(t => t.ID_PASSO).HasColumnName("ID_PASSO");
this.Property(t => t.DESCRICAO).HasColumnName("DESCRICAO");
}
}
public partial class FluxoPassoAcao
{
public int ID_FLUXO { get; set; }
public int ID_PASSO { get; set; }
public int ID_ACAO { get; set; }
public virtual Acao Acao { get; set; }
public virtual Fluxo Fluxo { get; set; }
public virtual Passo Passo { get; set; }
}
public class FluxoPassoAcaoMap : EntityTypeConfiguration<FluxoPassoAcao>
{
public FluxoPassoAcaoMap()
{
this.HasKey(t => new { t.ID_FLUXO, t.ID_PASSO, t.ID_ACAO });
this.Property(t => t.ID_FLUXO).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
this.Property(t => t.ID_PASSO).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
this.Property(t => t.ID_ACAO).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
this.ToTable("FluxoPassoAcao");
this.Property(t => t.ID_FLUXO).HasColumnName("ID_FLUXO");
this.Property(t => t.ID_PASSO).HasColumnName("ID_PASSO");
this.Property(t => t.ID_ACAO).HasColumnName("ID_ACAO");
this.HasRequired(t => t.Acao)
.WithMany(t => t.FluxoPassoAcaos)
.HasForeignKey(d => d.ID_ACAO);
this.HasRequired(t => t.Fluxo)
.WithMany(t => t.FluxoPassoAcaos)
.HasForeignKey(d => d.ID_FLUXO);
this.HasRequired(t => t.Passo)
.WithMany(t => t.FluxoPassoAcaos)
.HasForeignKey(d => d.ID_PASSO);
}
}
Generics class
public partial class GenericsContext : DbContext
{
public GenericsContext()
: base("Name=GenericsContext")
{
}
public DbSet<Acao> Acao { get; set; }
public DbSet<Fluxo> Fluxo { get; set; }
public DbSet<FluxoPassoAcao> FluxoPassoAcao { get; set; }
public DbSet<Passo> Passos { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new AcaoMap());
modelBuilder.Configurations.Add(new FluxoMap());
modelBuilder.Configurations.Add(new FluxoPassoAcaoMap());
modelBuilder.Configurations.Add(new PassoMap());
}
}
Insert
using (GenericsContext db = new GenericsContext())
{
Passo passo = new Passo();
passo.DESCRICAO = "Passo";
db.Passo.Add(passo);
db.SaveChanges();
Fluxo fluxo = new Fluxo();
fluxo.DESCRICAO = "Fluxo";
db.Fluxo.Add(fluxo);
db.SaveChanges();
Acao acao = new Acao();
acao.DESCRICAO = "Acao";
db.Acao.Add(acao);
db.SaveChanges();
FluxoPassoAcao fluxopassoacao = new FluxoPassoAcao();
fluxopassoacao.Acao = acao;
fluxopassoacao.Fluxo = fluxo;
fluxopassoacao.Passo = passo;
db.FluxoPassoAcao.Add(fluxopassoacao);
db.SaveChanges();
}
Search by Fluxopassoacao
FluxoPassoAcao findFPA = db.FluxoPassoAcao.Where(x =>x.ID_PASSO == 2 && x.ID_ACAO == 2 && x.ID_FLUXO == 2).FirstOrDefault();
Realize it works, but, uh, it will really depend on whether that’s what you need!
It depends on what you want to do, because it is not feasible the way you did and the tool generates over patterns...
– user6026
I’m sorry, Potter, but what mistake did I make? As I said, I’m learning and any guidance is very valid!
– Rubens
I really need to understand why 3 keys, a more detailed study your question is good but, it is kind of impossible to do so!
– user6026
will depend on what you want to implement, because, there are many for many is are two key fields only!
– user6026
I explain: it is the flow of my system. There are several flows and these flows have steps. Each step of this flow has several actions (activities) that will lead to different steps depending on which action was executed. That’s why the triple key. Help? Rsrs
– Rubens
Yes @Rubens the Gypsy response would be the solution to your system!!!
– user6026
i made an example going from base to Entity with Fluent.
– user6026
@Rubens Could you please accept the correct answer? This is done by clicking on the 'V' icon below the vote counter of each reply. Thank you!
– Leonel Sanches da Silva