Entity Framework Core relationship creation one to Many

Asked

Viewed 136 times

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");

    }
}

I want a relationship of one to many, where a sale receives several events, but the database does not recognize any of the forms of declaration with ICollection, IList, List or in the builder With Many.

  • The answer worked out for you?

1 answer

2

Did the wrong relationships, example, in class Evento belongs to a Venda, then your model lacks to explain this relation in the following way, follows example:

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; }
}

and apparently has Categoria and CasaShow also missing CategoriaId and CasaShowId because if not put and not relate it goes by convention when only puts the class.

In the Microsoft documentation this is well explained and you can understand the reasons why Framework do so.

In that link to conventions has the example of 1 for N amid Blog and 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; }
}

explaining how relationships should be and how to configure entities.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.