Error When Adding Controller in ASP.NET MVC

3

A question when creating a controller . Because trying to create it has presented the error of the attached image.

I created 3 small classes: Dependentes , Tipobenef , TitularPlano and ProvaContext

[Table("dependentes")]
public class Dependentes
{
    public int Id { get; set; }
    [Required]
    [MaxLength(100)]
    public string Nome { get; set; }
    //public Tipobenef Tipo { get; set; }
    [Required]
    [ForeignKey("TitularesPlano")]
    public int IdTitularPlano { get; set; }
    public virtual TitularPlano Titular { get; set; }
    public virtual List<Tipobenef> Tipobenefs { get; set; }

}

public enum Tipobenef
{    
    Filho,
    Conjugue,
    Pai,
    Mae        
}

[Table("Titularesplano")]
public class TitularPlano
{
    public int Id { get; set; }
    [Required]
    [MaxLength(100)]
    public string Nome { get; set; }
    public DateTime DataNascimento { get; set; }
    [Required]
    [MaxLength(11)]
    public string CPF { get; set; }
    [Required]
    [MaxLength(8)]
    public string RG { get; set; }
}

public class ProvaContext:DbContext
{
    public ProvaContext():base("Prova")
    {

    }
    public DbSet<TitularPlano> Titulares { get; set; }
    public DbSet<Dependentes> Dependentes { get; set; }

}

Here is the part of WebConfig :

<add name="Prova"
     providerName="System.Data.SqlClient"
     connectionString="Data Source=Localhost\DESKTOP-T8VEK67; Initial Catalog=DbProva; Integrated Security=True;"/>    

I already tried all the possible names of localhost

Image 1:

Image2:

    
asked by anonymous 02.01.2019 / 03:56

1 answer

2

Hi, Bruno, there is an error in the declaration of your foreign key.

You declare your column and put the FK for the entity designating the annotation for the attribute that makes the connection. In case you assigned a note that does not exist in the wrong attribute.

...    
public int IdTitularPlano { get; set; }

[ForeignKey("IdTitularPlano")]
public virtual TitularPlano Titular { get; set; }
...

I hope it helps.

    
02.01.2019 / 09:50