Tutorial by Examples

Key is a field in a table which uniquely identifies each row/record in a database table. Use this attribute to override the default Code-First convention. If applied to a property, it will be used as the primary key column for this class. using System.ComponentModel.DataAnnotations; public clas...
When applied to a property of a domain class, the database will create a NOT NULL column. using System.ComponentModel.DataAnnotations; public class Person { public int PersonID { get; set; } [Required] public string PersonName { get; set; } } The resulting column wi...
[MaxLength(int)] attribute can be applied to a string or array type property of a domain class. Entity Framework will set the size of a column to the specified value. using System.ComponentModel.DataAnnotations; public class Person { public int PersonID { get; set; } [MinLength(...
Specifies a numeric minimum and maximum range for a property using System.ComponentModel.DataAnnotations; public partial class Enrollment { public int EnrollmentID { get; set; } [Range(0, 4)] public Nullable<decimal> Grade { get; set; } } If we try to insert/upd...
Specifies how the database generates values for the property. There are three possible values: None specifies that the values are not generated by the database. Identity specifies that the column is an identity column, which is typically used for integer primary keys. Computed specifies that th...
By Code-First convention, Entity Framework creates a column for every public property that is of a supported data type and has both a getter and a setter. [NotMapped] annotation must be applied to any properties that we do NOT want a column in a database table for. An example of a property that we ...
[Table("People")] public class Person { public int PersonID { get; set; } public string PersonName { get; set; } } Tells Entity Framework to use a specific table name instead of generating one (i.e. Person or Persons) We can also specify a schema for the table using [T...
public class Person { public int PersonID { get; set; } [Column("NameOfPerson")] public string PersonName { get; set; } } Tells Entity Framework to use a specific column name instead using the name of the property. You can also specify the database data type a...
public class Person { public int PersonID { get; set; } public string PersonName { get; set; } [Index] public int Age { get; set; } } Creates a database index for a column or set of columns. [Index("IX_Person_Age")] public int Age { get; set; } This creates ...
Specifies custom foreign key name if a foreign key not following EF's convention is desired. public class Person { public int IdAddress { get; set; } [ForeignKey(nameof(IdAddress))] public virtual Address HomeAddress { get; set; } } This can also be used when you have multipl...
using System.ComponentModel.DataAnnotations; public class Post { public int Id { get; set; } [StringLength(100)] public string Title { get; set;} [StringLength(300)] public string Abstract { get; set; } public string Description { get; set; } } Def...
[TimeStamp] attribute can be applied to only one byte array property in a given Entity class. Entity Framework will create a non-nullable timestamp column in the database table for that property. Entity Framework will automatically use this TimeStamp column in concurrency check. using System.Compon...
This attribute is applied to the class property. You can use ConcurrencyCheck attribute when you want to use existing columns for concurrency check and not a separate timestamp column for concurrency. using System.ComponentModel.DataAnnotations; public class Author { public int AuthorId { ...
using System.ComponentModel.DataAnnotations.Schema; public class Department { ... public virtual ICollection<Employee> PrimaryEmployees { get; set; } public virtual ICollection<Employee> SecondaryEmployees { get; set; } } public class Employee { ...
using System.ComponentModel.DataAnnotations.Schema; [ComplexType] public class BlogDetails { public DateTime? DateCreated { get; set; } [MaxLength(250)] public string Description { get; set; } } public class Blog { ... public BlogDetails BlogDetail { get...

Page 1 of 1