Errores creando webApp MVC
Muy bien, estoy poniéndome al día con MVC + ASP.NET, igual con PHP. Ya antes había trabajado con esta arquitectura, solo que en proyectos ya construidos y trabajando con los controladores y los modelos.
Estoy llenando los huecos que me faltan como son el configurar un proyecto desde ceros, modificar las vistas, mezclar con jQuery para mayor dinamismo, y otras cosas más que se me están escapando por el momento.
Ya he creado proyectos desde ceros siguiendo tutoriales, ahora estoy desarrollando mis propios sistemas migrando loq ue ya tengo en otras arquitecturas y lenguajes.
- Creo un proyecto web con ASP.NET Core, específicamente una web App, asegurándome que sea la plantilla Aplicación Web apuntando a ASP.NET Core 1.1
- Creo los modelos de datos dentro de una carpeta llamada Models y el contexto de datos en otra carpeta con el nombre Data
- Agrego controladores y vistas utilizando los modelos y contexto de datos
- Modifico el
.csprojdel proyecto para incluir las herramientas de CLI para Entity Framework - ejecuto
dotnet restore - ejecuto
dotnet ef migrations add Initial(donde "Initial" es un nombre para la migración, este nombre es bueno cambiarlo según se modifiquen los modelos para tener un registro histórico en caso de requerir retroceder) - ¡No funciona
dotnet-ef! averiguo que me faltó
- Agrego los paquetes de Entity Framework ejecutando desde al administrador de paquetes la instrucción
install-package Microsoft.EntityFrameworkCore.SqlServer - En el archivo Startup.cs, dentro del método
ConfigureServicesagrego la instrucción
services.addDbContext<mi_contexto>(
options => options.UseSqlServer(
Configuration.GetConnectionString(
"nombre_de_mi_conexión")));
Antes de la instrucciónservices.addMvc();
- En el mismo archivo agrego
usingpara los espacios de nombreMicrosoft.EntityFrameworkCorey MiProyecto.Data - En el archivo appsettings.json agrego mi cadena de conexión
{ "ConnectionStrings": { "DefaultConnection": "Server=(localdb)\\mssqllocaldb; Database=nombre_db; Trusted_Connection=True; MultipleActiveResultSets=true" } } - Verifico cómo está escrito en el
csprojy veo que escribí mal, debe serDotNetCliToolReference
- Agrego los paquetes de Entity Framework ejecutando desde al administrador de paquetes la instrucción
- Vuelvo a ejecutar
dotenet ef migrationsy funciona a la perfección
Tos comentarios me ayudan a mejorar el contenido de mis publicaciones para que sean de mayor utilidad e interés. Agradezco tu aportación.
Okay, I'm catching up with MVC + ASP.NET, same with PHP. I had worked with this architecture before, but only on built projects, and worked with controllers and models.
I'm filling holes, like configure projects from nothing, modify views, mix with jQuery to improve dynamism, and other things that are escaping to me at the moment.
I have created projects of principle following the tutorials, now I am developing my own systems by migrating what I have in other architectures and languages.
- I create a web project with ASP.NET Core, specifically a web application, I ensure me that it is the Web Application template and is pointing to ASP.NET Core 1.1.
- I create the data models inside a folder called Models and the data context inside another folder called Data.
- I add new controlles and views using the data models and the datacontext.
- I modify the
.csprojfile of the project to include the CLI tools for "Entity Framework". - I execute the
dotenet restore. - I execute the
dotenet ef migrations Initial(where Initial it's a name that you should change after each model modify and in every execution of this command in order to have an historical record, so if you need walk back). dotnet-efdoesn't work! I try to find what I lacked.
- I add the Entity Framework packages by executing from the Package Administrator Window the instruction:
install-package Microsoft.EntityFrameworkCore.SqlServer. - In the Startup.cs file, inside the ConfigureServices method I add the instruction
services.addDbContext<my_context>(before the instruction
options => options.UseSqlServer(
Configuration.GetConnectionString(
"name_of_my_connection")));services.addMvc();. - In the same file I add the
usinginstructions for the namespacesMicrosoft.EntityFrameworkCoreandMyProject.Data - In the appsettings.json file I add my connection string
{ "ConnectionStrings": { "DefaultConnection": "Server=(localdb)\\mssqllocaldb; Database=db_name; Trusted_Connection=True; MultipleActiveResultSets=true" } - I verify who it is written in the
.csprojmy modifications and I see that I wrote wrong, it have to beDotNetCliToolReference
- I add the Entity Framework packages by executing from the Package Administrator Window the instruction:
- I execute again the
dotnet ef migrationsinstruction and it works perfectly
Comentarios
Publicar un comentario