The Data annotations attribute classes are used to decorate the classes or properties. These attributes help Entity Framework Code First to apply pre-defined rules on entity models
We can devide data annotations into the following categories
1. Database Schema related
2. Validation attributes.
The Timestamp attribute specifies the byte array (byte []) property / column that has a concurrency mode of "Fixed" in the model and it should be a Timestamp column in the stored model (database). It is not nullable.
https://docs.microsoft.com/en-us/aspnet/mvc/overview/getting-started/getting-started-with-ef-using-mvc/handling-concurrency-with-the-entity-framework-in-an-asp-net-mvc-application
Using an InverseProperty attribute, we can specify which navigation property should be returned.
public class Contact
{
public int ContactId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public User CreatedBy { get; set; }
public User UpdatedBy { get; set; }
}
public class User
{
public int UserId { get; set; }
public string UserName { get; set; }
public List ContactsCreated { get; set; }
public List ContactsUpdated { get; set; }
}
This model appears to fit the inverse navigation property convention for Entity Framework Core, but EF Core will raise an error when asked to map these relationships:
Unable to determine the relationship represented by navigation property 'Contact.CreatedBy' of type 'User'. Either manually configure the relationship, or ignore this property from the model.
The InverseProperty attribute is applied to the navigation properties in the User class to specify their corresponding inverse navigation properties in the Contact class:
public class Contact
{
public int ContactId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public User CreatedBy { get; set; }
public User UpdatedBy { get; set; }
}
public class User
{
public int UserId { get; set; }
public string UserName { get; set; }
[InverseProperty("CreatedBy")]
public List ContactsCreated { get; set; }
[InverseProperty("UpdatedBy")]
public List ContactsUpdated { get; set; }
}
https://www.tektutorialshub.com/data-annotations-inverseproperty-attribute-in-entity-framework/
[Index("IX_Name_DepartmentMaster", IsClustered = false)]
public string Name { get; set; }
We can devide data annotations into the following categories
1. Database Schema related
2. Validation attributes.
1. Database Schema related
1. Table Attribute :
Provides the alternate table name and specify the schema.
2. Column Attribute :
Provide alternate column name, order amd database type for column.3. Key Attribute :
By default a property with name Id or Class Name + Id is primary key in a class, but if you want to make another property as a primary key, you can use key attribute.4. Composite Keys :
Add keyy attribute to more than one column will behave as composite key.The Timestamp attribute specifies the byte array (byte []) property / column that has a concurrency mode of "Fixed" in the model and it should be a Timestamp column in the stored model (database). It is not nullable.
5. ConcurrencyCheck Attribute :
If you want to prevent conflict while updating data by two user than we use concurrency check for each filed , whose value we want to compare with old one before updating.https://docs.microsoft.com/en-us/aspnet/mvc/overview/getting-started/getting-started-with-ef-using-mvc/handling-concurrency-with-the-entity-framework-in-an-asp-net-mvc-application
6. Timestamp Attribute :
A timestamp value compare while updating the data to prevent conflict. Instead of marking each field concurrency check.7. ForeignKey Attribute :
This attribute specifies the foreign key for the Navigation property.8. InverseProperty Attribute :
Used when the same type takes part in multiple relationships.
Using an InverseProperty attribute, we can specify which navigation property should be returned.
public class Contact
{
public int ContactId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public User CreatedBy { get; set; }
public User UpdatedBy { get; set; }
}
public class User
{
public int UserId { get; set; }
public string UserName { get; set; }
public List
public List
}
This model appears to fit the inverse navigation property convention for Entity Framework Core, but EF Core will raise an error when asked to map these relationships:
Unable to determine the relationship represented by navigation property 'Contact.CreatedBy' of type 'User'. Either manually configure the relationship, or ignore this property from the model.
The InverseProperty attribute is applied to the navigation properties in the User class to specify their corresponding inverse navigation properties in the Contact class:
public class Contact
{
public int ContactId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public User CreatedBy { get; set; }
public User UpdatedBy { get; set; }
}
public class User
{
public int UserId { get; set; }
public string UserName { get; set; }
[InverseProperty("CreatedBy")]
public List
[InverseProperty("UpdatedBy")]
public List
}
https://www.tektutorialshub.com/data-annotations-inverseproperty-attribute-in-entity-framework/
9. Index Attribute :
The property of the model can be marked with an attribute and it should participate in a store index. The Index attribute allows us to create an index on one or more columns and we can specify the name of the index.[Index("IX_Name_DepartmentMaster", IsClustered = false)]
public string Name { get; set; }
No comments:
Post a Comment