2
votes

There might be something wrong in my model that I cannot figure out since I get the following error when trying to make a migration:

"An error occurred while calling method 'BuildWebHost' on class Program. Continuing without the application service provider. Error: Field 'k__BackingField' of entity type 'MapeoArticuloProveedor' is readonly and so cannot be set. Unable to create an object of type 'NSideoContext'. Add an implementation of 'IDesignTimeDbContextFactory' to the project, or see https://go.microsoft.com/fwlink/?linkid=851728 for additional patterns supported at design time."

Entities:

[Table("MapeosArticuloProveedor", Schema = "public")]
public class MapeoArticuloProveedor
{

    public string Codigo { get; set; }        

    public int? IdLadoDeMontaje { get; set; }

    [ForeignKey("IdLadoDeMontaje")]
    public virtual LadoDeMontajeMapeoArticulo LadoDeMontaje { get; }

}

[Table("LadosDeMontajeMapeosArticulos", Schema = "public")]
public class LadoDeMontajeMapeoArticulo
{

    public string Codigo { get; set; }

    public string Valor { get; set; }

}

What could it be?

2
Most likely LadoDeMontaje { get; }. Add set; like other properties.Ivan Stoev

2 Answers

3
votes

@WanneBDeveloper basically you're exposing the property since you made it public. A more conservative approach would be to set it as follows:

    public LadoDeMontajeMapeoArticulo LadoDeMontaje { get; private set; }

note the private keyword

Then the propery can only be set from within the class and not outside of it. Therefore you'll know which class is mutating it's state.

2
votes

this is your issue:

public virtual LadoDeMontajeMapeoArticulo LadoDeMontaje { get; }

Basically the error is saying you can't set the "LadoDeMontaje" once it is retrieved.

Simply change it to:

public virtual LadoDeMontajeMapeoArticulo LadoDeMontaje { get; set; }