user6084053
user6084053

Reputation:

Two primary keys class

I started doing a new project and im trying to adapt my database that i created before to the database on Entity Framework, i have 2 tables: Challenge and Coment and what i want to do is associate a coment to a challenge. What i did before using Entity Framework:

 public class Challenge
{
    public int ChallengeId { get; set; } // id associado ao desafio
    public string TypeWork { get; set; } // tipo de trabalho que o cliente pretende que seja feito
    public string Description { get; set; } // Descricao detalhada do desafio
    public decimal Value { get; set; } // Valor em pontos ou em dinheiro pago ao vencedor do desafio
    public DateTime DateCriation { get; set; } // data da criação do anuncio
    public DateTime DataEnd { get; set; } // data em que expira o desafio
    public int? WinnerSolution { get; set; } // id da solucao vencedora
    public int PaymentTypeId { get; set; } // chave estrangeira para tipo de pagamento
    public int ComentChallengeId { get; set; } // chave estrangeira para Comentario do desafio
    public virtual ICollection<TypePayment> TypePayment { get; set; } // coleccao de tipos de pagamento 

    public virtual ICollection<ComentChallenge> ComentChallenge { get; set; } // lista de comentarios associados ao desafio

}

ComentChallenge class

namespace CrowdTouring.Models
{
    public class ComentChallenge
    {
        [Key]
        [Column(Order = 1)]
        public int ComentChallengeId { get; set; } // id do comentario do desafio
        [Key]
        [Column(Order = 2)]
        public int ChallengeId { get; set; } // id do desafio
        public string Title { get; set; } // titulo do comentario
        public string Description { get; set; } // Descricao do comentario
        public DateTime date { get; set; } // data do comentario

    }
}

In the comentChallenge class i created 2 primary keys 1 to associate to the challenge and the other to associate to the coments about the challenge am i using the right approach how can i do that this way?

Upvotes: 2

Views: 714

Answers (1)

Tom Droste
Tom Droste

Reputation: 1324

I would suggest using the MobelBuilder in the DbContext to take care of it. Let it know that the two are related. I modified your code below and added an example of a context.

By using the conventions the foreignkey id's will automaticly be generated for you in the database and you can use the navigation properties in code to access the relations.

 public class ApplicationDbContext : DbContext
    {
        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);

            builder.Entity<Challenge>().HasMany(x => x.CommentChallenges).WithOne(y => y.Challenge);
        }
    }

public class Challenge
{
    public int Id { get; set; } // id associado ao desafio
    public string TypeWork { get; set; } // tipo de trabalho que o cliente pretende que seja feito
    public string Description { get; set; } // Descricao detalhada do desafio
    public decimal Value { get; set; } // Valor em pontos ou em dinheiro pago ao vencedor do desafio
    public DateTime DateCriation { get; set; } // data da criação do anuncio
    public DateTime DataEnd { get; set; } // data em que expira o desafio
    public int? WinnerSolution { get; set; } // id da solucao vencedora
    public int PaymentTypeId { get; set; } // chave estrangeira para tipo de pagamento
    public virtual ICollection<TypePayment> TypePayment { get; set; } // coleccao de tipos de pagamento 

    public virtual ICollection<ComentChallenge> CommentChallenges { get; set; } // lista de comentarios associados ao desafio

}

namespace CrowdTouring.Models
{
    public class ComentChallenge
    {
        public int Id { get; set; } // id do comentario do desafio
        public string Title { get; set; } // titulo do comentario
        public string Description { get; set; } // Descricao do comentario
        public DateTime date { get; set; } // data do comentario
        public virtual Challenge Challenge { get; set;}
    }
}

Upvotes: 4

Related Questions