1
public class Evento {
    public int Id { get; set; }
    public string Nome { get; set; }    
    public Categoria Categoria { get; set; }
    public CasaShow CasaShow { get; set; }
    public float QuantDeIngressos { get; set; }
    public DateTime Data { get; set; }
    public float ValorDoIngresso { get; set; }
    public DateTime Hora { get; set; }
    public bool Status { get; set; }
}

public class Venda {

    public int Id { get; set;}
    public IdentityUser  User { get; set; }        
    public List<Evento> Evento { get; set;}

}

public class ApplicationDbContext : IdentityDbContext
{

    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
        : base(options)
    {
    }

    public DbSet <Categoria> Categorias { get; set; }
    public DbSet <CasaShow> CasasShows { get; set; }    
    public DbSet <Evento> Eventos { get; set; }
    public DbSet <Venda> Vendas { get; set; }

    protected override void OnModelCreating (ModelBuilder builder) {
        base.OnModelCreating (builder);
        builder.Entity<Venda>().HasMany(x => x.Evento);
        //Change my AspNetUser table to User
        builder.Entity<IdentityUser> ().ToTable ("User");
        //Change my AspNetRoles to Role
        builder.Entity<IdentityRole> ().ToTable ("Role");

    }
}

Quero uma relação de one to many, onde uma venda recebe vários eventos, mas o banco de dados não reconhece nenhuma das formas de declaração com ICollection, IList, List ou no builder With Many.

1
  • Deu certo a resposta pra você?
    – novic
    15/02/2020 às 0:59

1 Resposta 1

2

Fez as relação errada, exemplo, na classe Evento pertence a uma Venda, então o seu modelo falta explicitar essa relação da seguinte forma, segue exemplo:

public class Evento {
    public int Id { get; set; }
    public string Nome { get; set; }    
    public Categoria Categoria { get; set; } // problemas
    public CasaShow CasaShow { get; set; } // problemas
    public float QuantDeIngressos { get; set; }
    public DateTime Data { get; set; }
    public float ValorDoIngresso { get; set; }
    public DateTime Hora { get; set; }
    public bool Status { get; set; }

    // isso configura a relação
    public int VendaId { get; set; }
    public virtual Venda Venda { get; set; }
}

e pelo visto tem Categoria e CasaShow também faltando CategoriaId e CasaShowId porque se não colocar e não relacionar ele vai pela convenção quando só coloca a classe.

Na documentação da Microsoft isso é bem explicado e você pode entender os motivos do Framework fazer assim.

Nesse link sobre Convenções tem o exemplo de 1 para N entre Blog e Post:

public class Blog
{
    public int BlogId { get; set; }
    public string Url { get; set; }

    public List<Post> Posts { get; set; }
}

public class Post
{
    public int PostId { get; set; }
    public string Title { get; set; }
    public string Content { get; set; }

    public int BlogId { get; set; }
    public Blog Blog { get; set; }
}

explicando como deveria ser as relações e como configurar as entidades.

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .