In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-03 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 "ASP.NET Core how to use EF for relational database modeling", the content is detailed, the steps are clear, the details are handled properly, I hope this "ASP.NET Core how to use EF for relational database modeling" article can help you solve your doubts, following the editor's ideas slowly in-depth, together to learn new knowledge.
1. Brief introduction
In general, the configuration in this section applies to relational databases. When you install the relational database provider, the extension methods shown here become available (because of the shared Microsoft.EntityFrameworkCore.Relational package).
two。 Table mapping
The table map identifies which table in the database should perform content query and save operations.
2.1 Agreement
By convention, each entity is set to map to a table with the same name as the DbSet attribute (entities in the exposed derived context). If the given DbSet entity does not contain it, the class name is used.
2.2 data comments
You can use data annotations to configure type mapping tables.
[Table ("blogs")] public class Blog {public int BlogId {get; set;} public string Url {get; set;}}
You can also specify the schema (database) to which the table belongs.
[Table ("blogs", Schema = "blogging")] public class Blog {public int BlogId {get; set;} public string Url {get; set;} 2.3Fluent API
You can use the familiar API to configure the table to which the type is mapped.
Class MyContext: DbContext {public DbSet Blogs {get; set;} protected override void OnModelCreating (ModelBuilder modelBuilder) {modelBuilder.Entity () .ToTable ("blogs");} public class Blog {public int BlogId {get; set;} public string Url {get; set;}}
You can also specify the schema (database) to which the table belongs.
ModelBuilder.Entity () ToTable ("blogs", schema: "blogging"); 3. Column mapping
Column mapping identifies which column data should be queried and saved in the database.
3.1 Agreement
By convention, each property will be set to map to a column with the same name as the attribute.
3.2 data comments
You can use data annotations to configure the column to which the attribute is mapped.
Namespace EFModeling.DataAnnotations.Relational.Column {class MyContext: DbContext {public DbSet Blogs {get; set;}} public class Blog {[Column ("blog_id")] public int BlogId {get; set;} public string Url {get; set;}} 3.3Fluent API
You can use the familiar API to configure the column to which the attribute is mapped.
Namespace EFModeling.FluentAPI.Relational.Column {class MyContext: DbContext {public DbSet Blogs {get; set;} protected override void OnModelCreating (ModelBuilder modelBuilder) {modelBuilder.Entity () .Property (b = > b.BlogId) .HasColumnName ("blog_id");} public class Blog {public int BlogId {get; set } public string Url {get; set;} 4. Data type
The data type refers to the database-specific type of the column to which the attribute is mapped.
4.1 Agreement
By convention, the database provider selects the data type based on the .NET type of the property. It also considers other metadata, such as the maximum length of the configuration, whether the property is part of the primary key, and so on. For example, the DateTime and nvarchar (max) of SQL Server are used as properties of the key.
4.2 data comments
You can use data annotations to specify precise data types for columns. For example, the following code configures Url as a non-unicode string with a maximum length of 200. Rating is 5 to 2 decimal places.
Public class Blog {public int BlogId {get; set;} [Column (TypeName = "varchar (200)")] public string Url {get; set;} [Column (TypeName = "decimal (5,2)")] public decimal Rating {get; set;}} 4.3Fluent API
You can also use the familiar API to specify the same data type for the column.
Class MyContext: DbContext {public DbSet Blogs {get; set;} protected override void OnModelCreating (ModelBuilder modelBuilder) {modelBuilder.Entity (eb = > {eb.Property (b = > b.Url) .HasColumnType ("varchar (200)"); eb.Property (b = > b.Rating) .HasColumnType ("decimal (5,2)");}} public class Blog {public int BlogId {get; set } public string Url {get; set;} public decimal Rating {get; set;}} 5. Primary key
Introduce primary key (primary key) constraints for keys of each entity type.
5.1 Agreement
By convention, the primary key in the database is named PK_.
5.2 data comments
You cannot use data annotations to configure specific aspects of a relational database for a primary key.
5.3Fluent API
You can use API to configure the name of the primary key (primary key) constraint in the database.
Class MyContext: DbContext {public DbSet Blogs {get; set;} protected override void OnModelCreating (ModelBuilder modelBuilder) {modelBuilder.Entity () .HasKey (b = > b.BlogId) .HasName ("PrimaryKey_BlogId");} public class Blog {public int BlogId {get; set;} public string Url {get; set;} 6. Default schema
If no schema is explicitly configured for the object, the default schema is the database schema in which the object will be created.
6.1 Agreement
By convention, the database provider chooses the default schema that is most appropriate. For example, Microsoft SQL Server will use the dbo schema, and sqlite will not use the schema (because sqlite does not support schemas).
6.2 data comments
You cannot use data annotations to set the default schema.
6.3Fluent API
You can use API to specify the default schema.
Class MyContext: DbContext {public DbSet Blogs {get; set;} protected override void OnModelCreating (ModelBuilder modelBuilder) {modelBuilder.HasDefaultSchema ("blogging");}} 7. Default value
If you insert a new row but no value is specified for the column, the default value for the column is the value to insert.
7.1 Agreement
By convention, no default values are configured.
7.2 data comments
You cannot use data annotations to set default values.
7.3Fluent API
You can use API to specify the default value of the property.
Class MyContext: DbContext {public DbSet Blogs {get; set;} protected override void OnModelCreating (ModelBuilder modelBuilder) {modelBuilder.Entity () .Property (b = > b.Rating) .HasDefaultValue (3);}} public class Blog {public int BlogId {get; set;} public string Url {get; set;} public int Rating {get; set;}
You can also specify the SQL fragment used to calculate the default value.
Class MyContext: DbContext {public DbSet Blogs {get; set;} protected override void OnModelCreating (ModelBuilder modelBuilder) {modelBuilder.Entity () .property (b = > b.Created) .HasDefaultValueSql ("getdate ()");}} public class Blog {public int BlogId {get; set;} public string Url {get; set;} public DateTime Created {get; set;} 8. Index (relational database)
Indexes in relational databases map to the same concepts as indexes in the core of the entity framework.
8.1 convention
By convention, the index is named IX__. For a composite index, it becomes a list of attribute names separated by an underscore.
8.2 data comments
You cannot configure an index using data annotations.
8.3Fluent API
You can use the familiar API to configure the name of the index.
Class MyContext: DbContext {public DbSet Blogs {get; set;} protected override void OnModelCreating (ModelBuilder modelBuilder) {modelBuilder.Entity () .HasIndex (b = > b.Url) .HasName ("Index_Url");} public class Blog {public int BlogId {get; set;} public string Url {get; set;}}
You can also specify a filter.
Class MyContext: DbContext {public DbSet Blogs {get; set;} protected override void OnModelCreating (ModelBuilder modelBuilder) {modelBuilder.Entity () .HasIndex (b = > b.Url) .HasFilter ("[Url] IS NOT NULL");}} public class Blog {public int BlogId {get; set;} public string Url {get; set;}}
Use the SQL Server provider EF to add a "IS NOT NULL" filter for all columns contained in the unique index for null. To override this convention, null can provide a value.
Class MyContext: DbContext {public DbSet Blogs {get; set;} protected override void OnModelCreating (ModelBuilder modelBuilder) {modelBuilder.Entity () .HasIndex (b = > b.Url) .IsUnique () .HasFilter (null);}} public class Blog {public int BlogId {get; set;} public string Url {get; set;}}
Include columns in the SQL Server index, and when all columns in the query are included in the index as key or non-key columns, you can configure the index by including columns to significantly improve query performance.
Class MyContext: DbContext {public DbSet Posts {get; set;} protected override void OnModelCreating (ModelBuilder modelBuilder) {modelBuilder.Entity () .HasIndex (p = > p.Url) .IncludeProperties (p = > new {p.Title, p.PublishedOn}) .HasName ("Index_Url_Include_Title_PublishedOn") }} public class Post {public int PostId {get; set;} public string Url {get; set;} public string Title {get; set;} public DateTime PublishedOn {get; set }} here, the article "how ASP.NET Core uses EF to model relational databases" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it. If you want to know more about related articles, please 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.