

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


Create and use DbContext object using (var context = new SchoolContext()) { //work with context here } Create Operation context.Students.Add(newStudentObj); context.SaveChanges(); // or context.Entry(newStudentObj).State = EntityState.Added; context.SaveChanges(); Update Operation context.Entry(updatedObj).State = EntityState.Modified; context.SaveChanges(); Delete Operation context.Entry(entityObj).State = EntityState.Deleted; context.SaveChanges(); Get the current State of an entity var state = context.Entry
Table Name
[Table(“name”)], [Table(“name”, schema=”name”)] Specifies the database table name and schema (optional) that a class is mapped to. [key] Specifies primary key property [key] [column(Order=1)] Specifies that a property is one of the key in composite primary key [column(“name”,order=)] [column(“name”,TypeName=)] Specifies a database column name, order and data type. [timestamp] Specifies data type of a column as a row version and makes it concurrency column. This column will be included in where clause of all updates command. It can be applied to only one property of entity class [ConcurrencyCheck] Specifies that a property participates in optimistic concurrency checks. It can be applied to multiple properties of entity class. [Required] Creates NOTNULL column for a property [MaxLength(number,Named Parameters)] [MinLength(number, Named Parameters)] Specifies maximum and minimum Length of a string property [StringLength(number, Named Parameters)] Specifies the size of a string property [ForeignKey(“name”)] Denotes a property used as a foreign key in a relationship. The annotation may be placed on the foreign key property and specify the associated navigation property name, or placed on a navigation property and specify the associated foreign key name. [NotMapped] Skips DB column creation for a property [InverseProperty(“name”)] Specifies the inverse of a navigation property that represents the other end of the same relationship. [ComplexType] Specifies complex type.
Entity to Table mapping modelBuilder.Entity
Set key property
Set composite key properties
Set column order
Set DB column datatype
Set Column name
Set parameter name in StoredProcedure
Set Null column
Set NotNull column
Set MaxLength for string column
Set fixed length property (char)
Set Unicode string column
Set decimal column precision
Set concurrency property:
Set Identity Column
[DatabaseGenerated(DatabaseGeneratedOption.Computed)] Sets DB column as computed [DatabaseGenerated(DatabaseGeneratedOption.Identity)] Sets DB column as identity [DatabaseGenerated(DatabaseGeneratedOption.None)] Sets DB column as none