DataAnnotations

System.ComponentModel.DataAnnotations.Schema attributes

Attribute Description
Table The database table and/or schema that a class is mapped to.
Column The database column that a property is mapped to.
ForeignKey Specifies the property is used as a foreign key in a relationship.
DatabaseGenerated Specifies how the database generates values for a property.
NotMapped Applied to properties or classes that are to be excluded from database mapping.
InverseProperty Specifies the inverse of a navigation property
ComplexType Denotes that the class is a complex type. *Not currently implemented in EF Core.

System.ComponentModel.Annotations attributes

Attribute Description
Key Identifies one or more properties as a Key
Timestamp Specifies the data type of the database column as rowversion
ConcurrencyCheck Specifies that the property is included in concurrency checks
Required Specifies that the property’s value is required
MaxLength Sets the maximum allowed length of the property value (string or array)
StringLength Sets the maximum allowed length of the property value (string or array)

Algunos enlaces:

Validando sin parar. Uso de DataAnnotations

https://www.learnentityframeworkcore.com/configuration/data-annotation-attributes

https://www.c-sharpcorner.com/article/model-validation-using-data-annotations-in-asp-net-mvc/

Alumno y Curso

En nuestro proyecto de Alumnos vamos a añadir la entidad Curso, con su Id y un campo nombre (String) y Creditos (int)

Un alumno puede pertenecer a un solo curso y un curso tendrá varios alumnos.

Añadiremos POR CÓDIGO el curso ‘DAW 1’ de 200 créditos. Y a ese curso añadimos los alumnos ‘Eva’, ‘Ana’ y ‘Juan’ con los campos DNI y mail que queramos.

Proveedores conexión

https://docs.microsoft.com/es-es/ef/core/providers/?tabs=dotnet-core-cli

Microsoft.EntityFrameworkCore.Sqlite De SQLite 3.7 en adelante Proyecto EF Core (Microsoft) 6.0 Documentación
Microsoft.EntityFrameworkCore.InMemory Base de datos en memoria de EF Core Proyecto EF Core (Microsoft) Limitaciones 6.0 Documentación
Microsoft.EntityFrameworkCore.Cosmos API de SQL de Azure Cosmos DB Proyecto EF Core (Microsoft) 6.0 Documentación
Npgsql.EntityFrameworkCore.PostgreSQL PostgreSQL Equipo de desarrollo de Npgsql 6.0 Documentación
Pomelo.EntityFrameworkCore.MySql MySQL, MariaDB Proyecto Pomelo Foundation

Ejemplo mysql:

https://jasonwatmore.com/post/2022/03/25/net-6-connect-to-mysql-database-with-entity-framework-core

Ejercicio CodeFirst

Crear una entidad Alumno con los campos:
Id
Nombre
DNI
Email

Y dad los pasos necesarios para añadir esta entidad a la tabla Pasiona

Ejercicio Regex

Con el código de los enlaces, hacer un programa que nos busque todas las imágenes de una página web.

Bonus track: Descargarlas en una carpeta

CodeFirst


public class Student
{
public int StudentId { get; set; }
public string Name { get; set; }
}

public class Course
{
public int CourseId { get; set; }
public string CourseName { get; set; }
}
public class SchoolContext : DbContext
{
public DbSet<Student> Students { get; set; }
public DbSet<Course> Courses { get; set; }

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(@"Server=.\SQLEXPRESS;Database=pasiona;Trusted_Connection=True;");
}
}