Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Entity Framework's method of using Fluent API configuration

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "the method of Entity Framework using Fluent API configuration". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

I. configure the primary key

To explicitly set a property as the primary key, use the HasKey method. In the following example, the HasKey method is used to configure the ProductId primary key for the Product type.

1. Add a new Product class using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace FluentAPI.Model {public class Product {public int ProductId {get; set;} public string ProductName {get; set;} public decimal Price {get; set;}} 2, and create a new ProductMap class to set the primary key using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks Using System.Data.Entity;using System.Data.Entity.ModelConfiguration;using FluentAPI.Model;namespace FluentAPI.Data.FluentAPIMap {public class ProductMap: EntityTypeConfiguration {public ProductMap () {/ / use the HasKey method to configure the ProductId primary key on the Product type. This.HasKey (p = > p.ProductId);} 3. View the database

2. Configure compound primary key

The following example configures the DepartmentID and Name properties to be combined primary keys of type Department.

1. Create the Department class: using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace FluentAPI.Model {public class Department {public int DepartmentId {get; set;} public string Name {get; set;} public decimal Budget {get; set;} public DateTime StartDate {get; set;}} 2, create the DepartmentMap class, which is used to set the compound primary key using FluentAPI.Model;using System Using System.Collections.Generic;using System.Data.Entity.ModelConfiguration;using System.Linq;using System.Text;using System.Threading.Tasks;namespace FluentAPI.Data.FluentAPIMap {public class DepartmentMap: EntityTypeConfiguration {public DepartmentMap () {/ / configure DepartmentId and Name as compound primary keys this.HasKey (p = > new {p. DepartmentId, p.Name}) using anonymous classes;} 3, view the database

Data migration using EF, and then view the database table

Turn off the identification of the numeric primary key

The identity DatabaseGeneratedOption of the numeric primary key is an enumerated value that has the following three values:

DatabaseGeneratedOption.None: turn off the numeric primary key.

DatabaseGeneratedOption.Identity: set the numerical key to grow automatically

DatabaseGeneratedOption.Computed: the value of the numeric primary key is calculated (values cannot be inserted in this column).

1. Set using FluentAPI.Model;using System;using System.Collections.Generic;using System.ComponentModel.DataAnnotations.Schema;using System.Data.Entity.ModelConfiguration;using System.Linq;using System.Text;using System.Threading.Tasks to disable the numeric key. Namespace FluentAPI.Data.FluentAPIMap {public class DepartmentMap: EntityTypeConfiguration {public DepartmentMap () {/ / configure DepartmentId and Name as compound primary keys this.HasKey using anonymous classes (p = > new {p. DepartmentId, p.Name}); / / the following example sets the DepartmentID property to System.ComponentModel.DataAnnotations.DatabaseGeneratedOption.None to indicate that the value is not generated by the database. This.Property (p = > p.DepartmentId) .HasDatabaseGeneratedOption (DatabaseGeneratedOption.None);} 2. The specified value to be displayed by the DepartmentId column when inserting the database table: INSERT INTO Departments VALUES (1, personnel Department, 12.3 GETDATE ()); fourth, specify the maximum length of the attribute

HasMaxLength can set the maximum length of the columns in the table.

Using FluentAPI.Model;using System;using System.Collections.Generic;using System.ComponentModel.DataAnnotations.Schema;using System.Data.Entity.ModelConfiguration;using System.Linq;using System.Text;using System.Threading.Tasks;namespace FluentAPI.Data.FluentAPIMap {public class DepartmentMap: EntityTypeConfiguration {public DepartmentMap () {/ / configure DepartmentId and Name as compound primary keys this.HasKey using anonymous classes (p = > new {p. DepartmentId, p.Name}) / / the following example sets the DepartmentID property to System.ComponentModel.DataAnnotations.DatabaseGeneratedOption.None to indicate that the value is not generated by the database. / / this.Property (p = > p.DepartmentId) .HasDatabaseGeneratedOption (DatabaseGeneratedOption.None); / / the following example sets the DepartmentID property to System.ComponentModel.DataAnnotations.DatabaseGeneratedOption.None to indicate that the value is automatically generated by the database. The / / this.Property (p = > p.DepartmentId) .HasDatabaseGeneratedOption (DatabaseGeneratedOption.Identity); / / Name attribute should not exceed 50 characters. If its value exceeds 50 characters, a DbEntityValidationException exception occurs. / / if Code First creates a database based on this model, it also sets the maximum length of the Name column to 50 characters. This.Property (p = > p.Name) .HasMaxLength (50);}} V. Configure the property as required using FluentAPI.Model;using System;using System.Collections.Generic;using System.ComponentModel.DataAnnotations.Schema;using System.Data.Entity.ModelConfiguration;using System.Linq;using System.Text;using System.Threading.Tasks Namespace FluentAPI.Data.FluentAPIMap {public class DepartmentMap: EntityTypeConfiguration {public DepartmentMap () {/ / configure DepartmentId and Name as compound primary keys this.HasKey using anonymous classes (p = > new {p. DepartmentId, p.Name}); / / the following example sets the DepartmentID property to System.ComponentModel.DataAnnotations.DatabaseGeneratedOption.None to indicate that the value is not generated by the database. / / this.Property (p = > p.DepartmentId) .HasDatabaseGeneratedOption (DatabaseGeneratedOption.None); / / the following example sets the DepartmentID property to System.ComponentModel.DataAnnotations.DatabaseGeneratedOption.None to indicate that the value is automatically generated by the database. The / / this.Property (p = > p.DepartmentId) .HasDatabaseGeneratedOption (DatabaseGeneratedOption.Identity); / / Name attribute should not exceed 50 characters. If its value exceeds 50 characters, a DbEntityValidationException exception occurs. / / if Code First creates a database based on this model, it also sets the maximum length of the Name column to 50 characters. This.Property (p = > p.Name) .HasMaxLength (50); / * Name attribute is required. If Name is not specified, a DbEntityValidationException exception occurs. If Code First creates a database based on this model, the column used to store this property will not be null. * / this.Property (p = > p.Name) .IsRequired ();}} VI. Specify that the CLR attribute is not mapped to the column using FluentAPI.Model;using System;using System.Collections.Generic;using System.ComponentModel.DataAnnotations.Schema;using System.Data.Entity.ModelConfiguration;using System.Linq;using System.Text;using System.Threading.Tasks in the database Namespace FluentAPI.Data.FluentAPIMap {public class DepartmentMap: EntityTypeConfiguration {public DepartmentMap () {/ / configure DepartmentId and Name as compound primary keys this.HasKey using anonymous classes (p = > new {p. DepartmentId, p.Name}); / / the following example sets the DepartmentID property to System.ComponentModel.DataAnnotations.DatabaseGeneratedOption.None to indicate that the value is not generated by the database. / / this.Property (p = > p.DepartmentId) .HasDatabaseGeneratedOption (DatabaseGeneratedOption.None); / / the following example sets the DepartmentID property to System.ComponentModel.DataAnnotations.DatabaseGeneratedOption.None to indicate that the value is automatically generated by the database. The / / this.Property (p = > p.DepartmentId) .HasDatabaseGeneratedOption (DatabaseGeneratedOption.Identity); / / Name attribute should not exceed 50 characters. If its value exceeds 50 characters, a DbEntityValidationException exception occurs. / / if Code First creates a database based on this model, it also sets the maximum length of the Name column to 50 characters. This.Property (p = > p.Name) .HasMaxLength (50); / * Name attribute is required. If Name is not specified, a DbEntityValidationException exception occurs. If Code First creates a database based on this model, the column used to store this property will not be null. * / this.Property (p = > p.Name). IsRequired (); / * the following example shows how to specify that properties of type CLR do not map to columns in the database. Ignore is equivalent to data annotation NotMapped * / this.Ignore (p = > p.Budget);} VII. Map the CLR attribute to a specific column in the database

HasColumnName can be used to set column names that map to columns in a database table.

Using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Data.Entity;using System.Data.Entity.ModelConfiguration;using FluentAPI.Model;using System.ComponentModel.DataAnnotations.Schema;namespace FluentAPI.Data.FluentAPIMap {public class ProductMap: EntityTypeConfiguration {public ProductMap () {/ / use the HasKey method to configure the ProductId primary key on the Product type. This.HasKey (p = > p.ProductId); / * the following example maps the Price CLR attribute to a ProductPrice database column. * / this.Property (p = > p.Price) .HasColumnName ("ProductPrice");} VIII. Configure whether the string attribute supports Unicode content.

The IsUnicode () method, which has two overloaded functions, can be used to set whether Unicode characters are supported.

1. Overloading without parameters. Unicode characters are supported by default.

2. Overload with parameters, which are Boolean values. True supports Unicode,false, but not Unicode.

Using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Data.Entity;using System.Data.Entity.ModelConfiguration;using FluentAPI.Model;using System.ComponentModel.DataAnnotations.Schema;namespace FluentAPI.Data.FluentAPIMap {public class ProductMap: EntityTypeConfiguration {public ProductMap () {/ / use the HasKey method to configure the ProductId primary key on the Product type. This.HasKey (p = > p.ProductId); / * the following example maps the Price CLR attribute to a ProductPrice database column. * / this.Property (p = > p.Price) .HasColumnName ("ProductPrice"); / * * by default, the string is Unicode (nvarchar in SQLServer). You can use the IsUnicode method to specify that the string should be of type varchar. * / this.Property (p = > p.PlaceOfOrigin) .IsUnicode (false);}

View the database column type:

9. Configure the data type of the database column

The HasColumnType method supports different representations that map to the same base type.

/ * the HasColumnType method supports different representations that map to the same base type. Using this method does not support any data conversion at run time. * Note that IsUnicode is the preferred way to set the column to varchar because it is independent of the database. * / this.Property (p = > p.Name) .HasColumnType ("varchar"); 10. Configure properties of complex types 1. Create a new class Course with an attribute of type Department: using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace FluentAPIApp.Model {public class Course {get; set;} public string Title {get; set;} public int Credits {get; set } public virtual Department Department {get; set;}} using FluentAPI.Model;using System;using System.Collections.Generic;using System.Data.Entity.ModelConfiguration;using System.Linq;using System.Text;using System.Threading.Tasks;namespace FluentAPI.Data.FluentAPIMap {public class CourseMap: EntityTypeConfiguration {public CourseMap () {/ * you can access properties of complex types using dot notation. The maximum length of the Name that sets the Department attribute in the Course class is 32 * / this.Property (p = > p.Department.Name) .HasMaxLength (32). All attributes that map the CLR entity type to a specific table / * Department in the database will be mapped to columns in the table named t _ Department. * / ToTable ("t_Department"); / * you can also specify the architecture name as follows: * / ToTable ("t_Department", "school"). This is the end of the content of "Entity Framework configuration using Fluent API". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report