c# -- ABP学习笔记 --- Entity关系设置

一 、表关系 

 

一对一一对多多对多

Entity:

Blog : 属性 BlogImage

BlogImage : 属性 BlogId ; Blog

Entity:

Blog : 属性 List<Post> Posts

Post : 属性 Blog,Blog

Entity:

Post : 属性 List<PostTag> PostTags

Tag : 属性 List<PostTag> PostTags

PostTag : 属性  PostId ; Post ; TagId ; Tag

配置 

Blog

     .HasOne(b => b.BlogImage)

     .WithOne(i => i.Blog)

     .HasForeignKey<BlogImage>(b => b.BlogId);

配置

Post

     .HasOne(p => p.Blog)

     .WithMany(b => b.Posts)

     .HasForeignKey(p => p.BlogId);

配置

PostTag

     .HasOne(pt => pt.Post)

     .WithMany(p => p.PostTags)

     .HasForeignKey(pt => pt.PostId);

PostTag

     .HasOne(pt => pt.Tag)

     .WithMany(t => t.PostTags)

     .HasForeignKey(pt => pt.TagId);

以一对多为示例,下面是配置文件的代码示例

eg:
public class PostConfiguration : IEntityTypeConfiguration<PostEntity>
    {
        public void Configure(EntityTypeBuilder<<PostEntity> builder)
        {
            builder.ToTable("<Post");

            builderPost.HasOne(p => p.Blog)
                       .WithMany(b => b.Posts)
                       .HasForeignKey(p => p.BlogId)
                       .OnDelete(DeleteBehavior.Cascade);

        }
    }

在 EntityTypeConfigurationRegister.cs文件的Register方法中添加modelBuilder.ApplyConfiguration(new PostConfiguration());

二、关系名词

  * 导航属性:主从实体中引用相关实体的属性

  * 导航 :HasOne, HasMany

  * 反向导航 :WithOne, WithMany

  * 引用导航 : HasOne, withOne

  * 集合导航 : HasMany, WithMany

 * 主体密钥:外键引用主体主键外属性

三,一对多关系对象的增删改查

以Entity:Blog(属性 List<Post> Posts )和 Post (属性 Blog,Blog)为例。

添加(添加Blog时自动将子对象Post值插入)

          new Blog();

          Blog.Posts = list;  // 赋值list中的post对象ID可为空

 调用Blog的InsertAsync( )方法即可将新建的Blog和Post对象插入数据库中,且自动添加关系ID值

删除(通过Blog的id删除Blog自动将关联的Post删除):需要手动删除Post再删除Blog。

         _postRepository.DeleteAsync(item => item.BlogId== id);

         _blogRepository.DeleteAsync(id)

查询(通过Blog的id查询Blog自动将关联的Post取出):

          var query = _blogRepository.GetAll().Where(item => item.Id == id)
                .Include(item => item.Posts);

          var entity = await query.ToListAsync();
          return entity.Count == 1 ? entity[0] : null;

修改(修改Blog和Post):

     支持对Blog的Posts中添加或者修改:

           foreach(var item in input.Posts){

                 _postRepository.InsertOrUpdateAsync(input.item);

           }

          _blogRepository.UpdateAsync(input)

四,一对多关系对象的深拷贝

创建EntityHelper类。

public static class BlogEntityHelper
    {
        public static Blog Clone(this Blog b)
        {
            if (b == null)
            {
                return null;
            }
            var blog = new Blog 
            {
                Title = b.Title
            };

           blog.Posts= b.Posts? .Select(item => item.Clone()).ToList();
           return blog;
        }
    }

public static class PostEntityHelper
    {
        public static Post Clone(this Post input)
        {
            if (input == null)
            {
                return null;
            }

            var post = new Post
            {
                Notes = input.Notes
            };

            return post ;
        }
    }

调用blog.Clone()即可返回一个深拷贝对象,调用Insert方法插入数据库即可。

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值