Part-20: Data annotations in entity framework core | Entity framework core 7.0 tutorial |ef core

Опубликовано: 09 Октябрь 2024
на канале: Just Pick and Learn
2,125
71

Data annotations are the attributes used on the model properties to customize the behavior. In this video you will understand how to use data annotation attributes to customize the metadata and behavior for your database entities.

Here are short descriptions of most used EF Core data annotation attributes:

1. [Table("Manager")]: Specifies the table name for an entity in the database as "Manager".

2. [Required]: Indicates that the annotated property is required and cannot be null.

3. [NotMapped]: Instructs EF Core to ignore the annotated property and not map it to a database column.

4. [Table("blogs", Schema = "blogging")]: Specifies the table name ("blogs") and schema ("blogging") for an entity in the database.

5. [Comment("Blogs managed on the website")]: Provides a comment or description for the annotated entity or property, in this case, indicating that it represents blogs managed on the website.

6. [Column("blog_id")]: Specifies the column name for a property in the database as "blog_id".

7. [Column(TypeName = "varchar(200)")]: Defines the column type for a property as "varchar(200)".

8. [MaxLength(500)]: Sets the maximum length of a string property to 500 characters.

9. [Precision(3)]: Specifies the precision for a numeric property, indicating the number of digits that can be stored.

10. [Unicode(false)]: Indicates that a string property should use non-Unicode (ASCII) encoding.

11. [Column(Order = 1)]: Specifies the order of the column in the table, in this case, with an order value of 1.

12. [Key]: Marks a property as a primary key for the entity.

13. [PrimaryKey(nameof(State), nameof(LicensePlate))]: Specifies a composite primary key for an entity using the properties "State" and "LicensePlate".

14. [DatabaseGenerated(DatabaseGeneratedOption.Identity)]: Indicates that the value for a property is generated by the database, typically an auto-incrementing identity column.

15. [ForeignKey(nameof(Blog))]: Establishes a foreign key relationship with the "Blog" entity, using the "Blog" property as the foreign key.

16. [InverseProperty("Blog")]: Specifies the inverse navigation property for a relationship, in this case, indicating that the "Blog" property in the related entity is the inverse of this property.

17. [DeleteBehavior(DeleteBehavior.Restrict)]: Specifies the delete behavior for a relationship when the related entity is deleted, in this case, restricts deletion if there are related entities.

18. [Index(nameof(Url))]: Creates an index on the specified column ("Url") in the database, which can improve query performance.


Various playlists from this channel:
Entity Framework core tutorial:
   • Entity Framework Core 7.0 tutorial  

Asp.net core tutorial playlist:
   • Asp.net Core tutorial for beginners  

C# tutorial:
   • C# Basics  

C# programs for practice:
   • C# - Logical programs  

.NET fundamentals:
   • .NET fundamentals  

#Entity Framework core 7.0 tutorial
#Ef core
#e f core
#Data annotations in ef core