In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
In this article Xiaobian for you to introduce in detail "how to create a model using EF in ASP.NET Core", the content is detailed, the steps are clear, and the details are handled properly. I hope that this article "how to create a model using EF in ASP.NET Core" can help you solve your doubts.
1. Required and optional attributes
If an entity attribute can contain null, it is considered optional. If the valid value of an attribute cannot contain null, it is considered a required attribute. When mapping to a relational database schema, the required attributes are created as columns that are not null, while optional attributes are created as columns that can be null.
1.1 Agreement
By convention, properties that the. NET type can contain null will be configured as optional, while .NET types that do not contain null will be configured as needed. For example, all properties with .net value types (int, decimal, bool, etc.) are required, and have .net value types that can be null (int?, decimal?, bool? All properties of) are configured as optional.
1.2 data annotations
You can configure the property that the Convention can be optional as required as follows:
Namespace EFModeling.DataAnnotations.Required {class MyContext: DbContext {public DbSet Blogs {get; set;}} public class Blog {public int BlogId {get; set;} / / with this annotation, this value must be written to [Required] public string Url {get; set;}} 1.3Fluent APInamespace EFModeling.FluentAPI.Required {class MyContext: DbContext {public DbSet Blogs {get; set } protected override void OnModelCreating (ModelBuilder modelBuilder) {modelBuilder.Entity () .property (b = > b.Url) / / this method means that .IsRequired ();}} public class Blog {public int BlogId {get; set;} public string Url {get; set;}} 2. Maximum length
Configuring the maximum length provides a hint for the data store about the appropriate data type to use for a given attribute. The maximum length applies only to array data types, such as string and byte []. For example, if the traditional data length of the front end is much larger than the limited length, it will be prompted.
2.1 Agreement
By convention, it is up to the database provider to select the appropriate data type for the property, that is, the length of the database field is limited when the value is accepted by the production program entity. For properties with length, the database provider will typically select the data type that allows the longest data length. For example, Microsoft SQL Server will use nvarchar (max) for the character string attribute (or nvarchar if the column is used as a key).
2.2 data annotations
You can use data annotations to configure the maximum length for attributes. This example is targeted at SQL Server, so the data type nvarchar (500) is used.
Namespace EFModeling.DataAnnotations.MaxLength {class MyContext: DbContext {public DbSet Blogs {get; set;}} public class Blog {public int BlogId {get; set;} / / set the maximum length [MaxLength] public string Url {get; set;}} 2.3Fluent APInamespace EFModeling.FluentAPI.MaxLength {class MyContext: DbContext {public DbSet Blogs {get; set } protected override void OnModelCreating (ModelBuilder modelBuilder) {modelBuilder.Entity () .Property (b = > b.Url) / / set the maximum length .HasMaxLength;}} public class Blog {public int BlogId {get; set;} public string Url {get; set;}} 3. Concurrent tagging
When we find that an entity field in a production environment is often concurrent, we can annotate it as a concurrent field.
3.1 Agreement
By convention, properties are never configured as concurrent tags.
3.2 data comments
You can use data annotations to configure properties as concurrent tags.
Public class Person {public int PersonId {get; set;} / / concurrent tag [ConcurrencyCheck] public string LastName {get; set;} public string FirstName {get; set;} 3.3Fluent API
You can use the familiar API to configure attributes as concurrent tags.
Class MyContext: DbContext {public DbSet People {get; set;} protected override void OnModelCreating (ModelBuilder modelBuilder) {modelBuilder.Entity () .Property (p = > p.LastName) / / concurrent tag .IsConcurrencyToken ();}} public class Person {public int PersonId {get; set;} public string LastName {get; set;} public string FirstName {get; set;} 4. Timestamp / line version
The timestamp is an attribute type, and each time a row is inserted or updated, the database generates a new value. This attribute type is also treated as a concurrent tag. This ensures that you will receive abnormal messages when you and others modify the row data.
4.1 Agreement
By convention, the property is never configured as a timestamp.
4.2 data comments
You can use data annotations to configure properties as timestamps.
Public class Blog {public int BlogId {get; set;} public string Url {get; set;} / / set timestamp 1649324459 public byte [] Timestamp {get; set;}} 4.3Fluent API
You can use the familiar API to configure the property as a timestamp.
Class MyContext: DbContext {public DbSet Blogs {get; set;} protected override void OnModelCreating (ModelBuilder modelBuilder) {modelBuilder.Entity () .Property (p = > p.Timestamp) / / set the timestamp .IsRowVersion ();}} public class Blog {public int BlogId {get; set;} public string Url {get; set;} public byte [] Timestamp {get; set;} 5. Shadow attribut
Shadow attributes are useful when data in the database should not be exposed on mapped entity types. They are most commonly used for foreign key attributes, where the relationship between two entities is represented by foreign key values in the database, but using navigation attributes between entity types to manage relationships on entity types, shadow property values can be obtained and changed through ChangeTracker API:
Context.Entry (myBlog) .property ("LastUpdated") .CurrentValue = DateTime.Now
You can reference shadow properties in a LINQ query through the EF.Property static method:
Var blogs = context.Blogs.OrderBy (b = > EF.Property (b, "LastUpdated"))
If a relationship is found, but no foreign key attribute is found in the dependent entity class, you can create a shadow property by convention. In this case, the shadow foreign key attribute is introduced. The shadow foreign key attribute is named (navigation on the dependent entity that points to the principal entity is used for naming). If the principal key property name contains the name of the navigation property, the name will be just. If there is no navigation attribute on the dependent entity, the principal type name is used in its location.
For example, the following code list causes BlogId Post to introduce shadow attributes to the entity.
Class MyContext: DbContext {public DbSet Blogs {get; set;} public DbSet Posts {get; set;} public class Blog {public int BlogId {get; set;} public string Url {get; set;} / / Shadow attribute public List Posts {get; set;}} public class Post {public int PostId {get; set;} public string Title {get; set;} public string Content {get; set } / / Shadow properties public Blog Blog {get; set;}} 5.2 data comments
You cannot create shadow properties through data annotations.
5.3Fluent API
You can use "familiar with API" to configure shadow properties. Once you have called the string overload of the Property method, you can link to any configuration call to other properties. If you provide the name of the Property method that matches the name of an existing property (a shadow property or an attribute defined in the entity class), the code configures the existing property instead of introducing a new shadow property.
Class MyContext: DbContext {public DbSet Blogs {get; set;} protected override void OnModelCreating (ModelBuilder modelBuilder) {modelBuilder.Entity () / / create shadow property .Property ("LastUpdated");}} public class Blog {public int BlogId {get; set;} public string Url {get; set }} here, the article "how to use EF to create models in ASP.NET Core" has been introduced. If you want to master the knowledge of this article, you still need to practice and use it. If you want to know more about related articles, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.