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

How to use configuration partner to create database in Entity Framework

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Most people do not understand the knowledge points of this article "how to use configuration partners to create a database in Entity Framework", so the editor summarizes the following, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to use configuration partners to create a database in Entity Framework" article.

EF provides another way to solve this problem by creating a separate configuration class for each entity class. These configuration partner classes are then called in the OnModelCreating method.

Create a Product entity class:

Using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Data.Entity.ModelConfiguration;namespace EF configuration partner .Model {public class Product {public int ProductNo {get; set;} public string ProductName {get; set;} public double ProductPrice {get; set;}}

Create the configuration class of the Product entity class: ProductMap. The configuration class needs to inherit from the EntityTypeConfiguration generic class. EntityTypeConfiguration is located under the System.Data.Entity.ModelConfiguration namespace, and the ProductMap class is as follows:

Using EF configuration partner .Model; using System;using System.Collections.Generic;using System.Data.Entity.ModelConfiguration;using System.Linq;using System.Text;namespace EF configuration partner .EDM {public class ProductMap: EntityTypeConfiguration {public ProductMap () {/ / sets the generated table name ToTable ("ProductConfiguration") / / set the primary key this.HasKey (p = > p.ProductNo) of the generated table; / / modify the generated column name this.Property (p = > p.ProductNo) .HasColumnName ("Id"); this.Property (p = > p.ProductName) .IsRequired () / / set the ProductName column is required. HasColumnName ("Name") / / Map ProductName to the Name column of the data table}

Called in the OnModelCreating () method of the data context Context class:

Using EF configuration partner .EDM; using EF configuration partner .Model; using System;using System.Collections.Generic;using System.Data.Entity;using System.Linq;using System.Text;namespace EF configuration partner .EFContext {public class Context:DbContext {public Context (): base ("DbConnection") {} public DbSet Products {get; set } protected override void OnModelCreating (DbModelBuilder modelBuilder) {/ / add the configuration class modelBuilder.Configurations.Add (new ProductMap ()) of the Product class; base.OnModelCreating (modelBuilder);}

Looking at the database, you can see that the changes match us:

This method of writing is almost the same as using modelBuilder, except that this method is better organized to deal with multiple entities. You can see that the above syntax is the same as chain programming for writing jQuery, which is called Fluent API.

The above is about the content of this article on "how to use configuration partners to create a database in Entity Framework". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, 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: 277

*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