. Net ef core design code conversion (Part 1), efcore

Source: Internet
Author: User

. Net ef core design code conversion (Part 1), efcore
 I. Preface

The. net core 2.0 official version has been released for a few months. After research, I decided to transfer the project. If you are a newbie, you can read some official introductions first.

Transport: https://docs.microsoft.com/zh-cn/dotnet/core/

I encountered some pitfalls in the design model of the domain, so I would like to share some of my solutions with you.

 

OK. The general structure is as follows:

Compare the textbook architecture.

 

2. Domain Layer

Domain entity

 

Value Object

 

Protocol Interface

 

Work Unit Interface

 

 

Warehousing Interface

 

Aggregation and division, we first create a simple user entity

 

Iii. storage layer

Reference Microsoft. EntityFrameworkCore. Sqlite

 

Work Unit ISql Interface

 

Work Unit IQueryableUnitOfWork Interface

 

 

Next we will focus on the changes to the constructor:

 

OnModelCreating changes:

Previous settings

 

At present, ef core does not provide such convenient encapsulation. You need to implement it yourself.

Database Table Name Change:

public static void SetSimpleUnderscoreTableNameConvention(this ModelBuilder modelBuilder){  foreach (IMutableEntityType entity in modelBuilder.Model.GetEntityTypes())  {    Regex underscoreRegex = new Regex(@"((?<=.)[A-Z][a-zA-Z]*)|((?<=[a-zA-Z])\d+)");    entity.Relational().TableName = underscoreRegex.Replace(entity.DisplayName(), @"$1$2").ToLower();  }}

 

Disable cascading deletion of all primary and foreign key relationships

public static void SetOneToManyCascadeDeleteConvention(this ModelBuilder modelBuilder){    foreach (var relationship in modelBuilder.Model.GetEntityTypes().SelectMany(e => e.GetForeignKeys()))    {        relationship.DeleteBehavior = DeleteBehavior.Restrict;    }}

 

Fluent Api Configuration

public abstract class EntityTypeConfiguration<TEntity> where TEntity : class{    public abstract void Map(EntityTypeBuilder<TEntity> builder);}
public static void AddConfiguration<TEntity>(this ModelBuilder modelBuilder, EntityTypeConfiguration<TEntity> configuration) where TEntity : class{    configuration.Map(modelBuilder.Entity<TEntity>());}
public class UserEntityTypeConfiguration : EntityTypeConfiguration<User>{    public override void Map(EntityTypeBuilder<User> builder)    {        builder.HasKey(u => u.Id);    }}

 

When used, it is added in this way, and there is no sense of violation.

 

The SQL query method has changed.

 

Pay attention to the type of constructor when implementing User table warehousing. This will be involved in. net core dependency injection.

 

Iv. Application Layer

The Application Layer does not change, so you can directly paste the code

 

V. distributed service layer

. Net core comes with a simple Ioc, so the previously used framework similar to Unity can be removed.

How does Configuration come from this?

. Net core is now recommended to use Json and other configuration files, which are very nice and powerful to use.

Then we need to ensure that the database does not exist when it is created correctly

UserController code:

Vi. Presentation Layer

A simple look at the WPF Interface

Background code:

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.