Entity Framework 实体关系总结:one-to-one, one-to-many, many-to-many

Entity Framework 实体关系总结:one-to-one, one-to-many, many-to-many

通过 Entiy Framework实践系列 文章,理了理 Entity Framework 的实体关系。

https://www.cnblogs.com/dudu/archive/2011/07/11/ef_one-to-one_one-to-many_many-to-many.html

为什么要写文章来理清这些关系?“血”的教训啊,刚开始使用 Entity Framework 的时候,由于没有静下心来认真理清关系,走了一些"痛不欲生"的弯路。而我们目前开发的项目都在使用 Entity Framework,为了避免其他人再经历"痛不欲生"的弯路。于是下定决心边“理清关系”边“写博客”。而写博客可以逼着自己把问题完整地解决,避免半途而废。当写出这些文章,自己不知不觉对问题有了更深的理解。

温故而知新,通过这篇总结将自己对EF实体关系的理解回锅热一热,也许会有新的收获;感情也一样,当感情冷下来的时候,别忘了回锅热一热。

1. 一对一关系(one-to-one)

a) 单向一对一(文章链接

类图:

数据库表结构:

Entity Framework中实体关系的定义:

 
   
modelBuilder.Entity < BlogSite > ()
.HasRequired(b
=> b.BlogUser)
.WithMany()
.HasForeignKey(b
=> b.UserID);

b) 双向一对一(文章链接

类图:

数据库表结构:

Entity Framework中实体关系的定义:

复制代码
 
   
modelBuilder.Entity < BlogSite > ()
.HasRequired(b
=> b.BlogUser)
.WithMany()
.HasForeignKey(b
=> b.UserID);

modelBuilder.Entity
< BlogUser > ()
.HasRequired(u
=> u.BlogSite)
.WithMany()
.HasForeignKey(u
=> u.BlogID);
复制代码


2. 一对多关系(one-to-many,文章链接

类图:

数据库表结构:

Entity Framework中实体关系的定义:
 
   
modelBuilder.Entity < BlogSite > ()
.HasMany(b
=> b.BlogPosts)
.WithRequired(p
=> p.BlogSite);


3. 多对多关系(many-to-many,文章链接

类图:

数据库表结构:

Entity Framework中实体关系的定义:

复制代码
 
   
modelBuilder.Entity < BlogPost > ()
.HasMany(b
=> b.Categories)
.WithMany(c
=> c.BlogPosts)
.Map
(
m
=>
{
m.MapLeftKey(
" BlogPostID " );
m.MapRightKey(
" CategoryID " );
m.ToTable(
" BlogPost_Category " );
}
);
复制代码
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MyBatis-Plus is an enhanced version of MyBatis, a popular open-source Java persistence framework. It provides additional features and utilities to simplify database operations. To perform a pagination query using MyBatis-Plus, you can use the `selectPage` method provided by the `com.baomidou.mybatisplus.core.mapper.BaseMapper` interface. Here's an example of how to use the `selectPage` method: ```java import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.core.metadata.IPage; // Assuming you have a mapper interface that extends BaseMapper public interface YourMapper extends BaseMapper<YourEntity> { } // In your service or repository class @Autowired private YourMapper yourMapper; public IPage<YourEntity> getEntityPage(int pageNo, int pageSize) { // Create a Page object with the desired page number and page size Page<YourEntity> page = new Page<>(pageNo, pageSize); // Create a QueryWrapper object for your query conditions (if any) QueryWrapper<YourEntity> queryWrapper = new QueryWrapper<>(); // Perform the pagination query using selectPage return yourMapper.selectPage(page, queryWrapper); } ``` In the above example, `YourEntity` represents your entity class, `YourMapper` is the mapper interface extending `BaseMapper`, and `yourMapper` is an instance of the mapper interface injected using the `@Autowired` annotation. The `getEntityPage` method takes two arguments: `pageNo` for the desired page number and `pageSize` for the number of records per page. It creates a `Page` object with these parameters and a `QueryWrapper` object for any query conditions. Finally, it calls the `selectPage` method on the mapper interface to perform the pagination query and returns the resulting `IPage` object. You can customize the `QueryWrapper` object to add various query conditions such as equals, greater than, less than, etc., depending on your requirements. Please note that this is just a basic example, and there are many other features and options available in MyBatis-Plus for pagination and database operations. You can refer to the official documentation for more details: [MyBatis-Plus Documentation](https://mybatis.plus/)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值