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 DbModelBuilder API to create Table structure in Entity Framework

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

Share

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

This article mainly explains "how to use DbModelBuilder API in Entity Framework to create table structure", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "how to use DbModelBuilder API in Entity Framework to create table structure" bar!

The DbContext class has an OnModelCreating method that is used to fluently configure the mapping of domain classes to database schemas. Let's define the mapping in a fluent API way.

First, comment out the Product class, rewrite the class, and rewrite the Product class:

Using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace EFFluentAPI.Model {public class Product {public int ProductNo {get; set;} public string ProductName {get; set;} public double ProductPrice {get; set;}

Then use fluent API in the OnModelCreating method in the database context Context class to define the database schema for the Product table:

Using EFFluentAPI.Model;using System;using System.Collections.Generic;using System.Data.Entity;using System.Linq;using System.Text;using System.Threading.Tasks;namespace EFFluentAPI.EFContext {public class Context:DbContext {public Context (): base ("DbConnection") {} public DbSet Products {get; set } / re-OnModelCreating method / protected override void OnModelCreating (DbModelBuilder modelBuilder) {/ / map to tables Product,ProductNo and ProductName as compound primary keys modelBuilder.Entity (). ToTable ("Product") .HasKey (p = > new {p.ProductNotem P.ProductName}) / / the column name that ProductNo maps to the data table is Id modelBuilder.Entity () .Property (p = > p.ProductNo) .HasColumnName ("Id") ModelBuilder.Entity () .Property (p = > p.ProductName) .IsRequired () / / setting ProductName is required, that is, it cannot be null. By default, you can set the ProductName column Unicode character to the .IsUnicode () / / of null. In fact, the default is the Unicode character, so this method can not be written. .HasMaxLength (10); / / sets the maximum length of the ProductName column to 10 base.OnModelCreating (modelBuilder);}

ModelBuilder.Entity () gets an instance of the EntityTypeConfiguration class. In addition, an important determinant of using fluent API is whether we use an external POCO class, that is, whether the entity model class comes from a class library. We cannot modify the definition of the class in the class library, so we cannot provide mapping details through data annotations. In this case, we must use fluent API.

What is POCO?

POCO refers to Plain Old Class Object, that is, the most basic CLR Class. In the original EF, entity classes were usually inherited from a base class with a large number of attribute descriptions. POCO refers to the original Class, in other words, the Class of this entity only needs to inherit from Object, not from a particular base class. It is mainly used with Code First. Code First means that we first define an entity Class like POCO, and then generate a database. In fact, you can now use Entity Framweork Power tools to reverse the existing database into POCO's Class (not through the edmx file).

The database created after the program runs is shown in the following figure:

Thank you for your reading, the above is the content of "how to use DbModelBuilder API to create table structure in Entity Framework". After the study of this article, I believe you have a deeper understanding of how to use DbModelBuilder API to create table structure in Entity Framework. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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