Entity Framework Core 2.0 - Error Migration "Field of entity type is readonly and so cannot be set"

entity-framework-core

Question

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?

1
2
2/14/2018 5:09:55 PM

Popular Answer

@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.

3
2/12/2019 1:02:12 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow