【问题标题】:Example of CRUD operations on classes with many-to-many relationship | Entity Framework Core多对多关系类的CRUD操作示例 |实体框架核心
【发布时间】:2019-07-28 13:46:29
【问题描述】:

假设我有以下与自身具有多对多关系的类:

public class A
{
    public int Id { get; set; }
    public string Title { get; set; }
    public ICollection<A> Requires{ get; set; }
    public ICollection<A> Blocks{ get; set; }
}

从这个answer我了解到我需要通过以下方式更改我的类以实现关系:

public class A
{
    public int Id { get; set; }
    public string Title { get; set; }
    public ICollection<ARelation> Requires{ get; set; }
    public ICollection<ARelation> Blocks{ get; set; }
}

public class ARelation
{
    public int BlockedId { get; set; }
    public int RequiredId{ get; set; }
    public virtual A Blocked { get; set; }
    public virtual A Required{ get; set; }
}

及配置文件:

modelBuilder.Entity<ARelation>()
            .HasKey(e => new { e.BlockedId, e.RequiredId });

modelBuilder.Entity<ARelation>()
            .HasOne(e => e.Blocked)
            .WithMany(e => e.Requires)
            .HasForeignKey(e => e.BlockedId);

modelBuilder.Entity<ARelation>()
            .HasOne(e => e.Required)
            .WithMany(e => e.Blocks)
            .HasForeignKey(e => e.RequiredId);

直到这里一切都很好。

我的问题是我没有找到关于如何进行 CRUD 操作的合适示例。

例如,假设我要存储一个新的A对象,我会使用以下操作:

var item = new A{
   Title = "Test"
}
_context.AObjects.Add(item);
await _context.SaveChangesAsync();

现在假设我要添加一个需要之前创建的对象的新 A 对象,我应该遵循什么程序?

GetAll 结果的大致样子示例:

 [{
   Title: "Test"
   Blocks: [{
        Title: "Test2"
   }]
 },
 { 
   Title: "Test2"
   Requires: [{
        Title: "Test"
   }]
 }]

我需要创建对象 ARelation 还是自动创建?

有人可以提供一个操作示例吗?

【问题讨论】:

    标签: entity-framework asp.net-core many-to-many crud


    【解决方案1】:

    假设您添加了一些A 对象:

    Id  Title
    1   Test
    2   Test2
    3   hello
    4   world
    

    您可以在创建新的A 对象时使用以下代码添加RequiresBlocks

    var item = new A
            {
                Title = "TestAgain",
                Blocks = new List<ARelation>()
                {
                    new ARelation()
                    {
                        BlockedId = 1
                    },
                    new ARelation()
                    {
                        BlockedId = 2
                    }
                },
                Requires = new List<ARelation>()
                {
                    new ARelation()
                    {
                        RequiredId = 3
                    },
                    new ARelation()
                    {
                        RequiredId = 4
                    }
                }
            };
            _context.AObjects.Add(item);
            await _context.SaveChangesAsync();
    

    当您想获取所有 A 对象并仅显示其标题时,您可以将 IncludeThenIncludeSelect 方法一起使用:

    [HttpGet]
    public ActionResult GetAll()
        {
           var x =  _context.AObjects
                            .Include(a => a.Requires)
                                   .ThenInclude(r => r.Required)
                            .Include(a => a.Blocks)
                                    .ThenInclude(b => b.Blcoked)
                            .Select(a => new 
                        {
                            Title = a.Title,
                            Requires = a.Requires.Select(r => new { Title = r.Required.Title}).ToList(),
                            Blocks = a.Blocks.Select(r => new { Title = r.Blocked.Title }).ToList()
                        }).ToList();
    
    
            return new JsonResult(x);
        }
    

    结果:

     [{
        "title": "TestAgain",
        "requires": [
            {
                "title": "hello"
            },
            {
                "title": "world"
            }
        ],
        "blocks": [
            {
                "title": "Test"
            },
            {
                "title": "Test2"
            }
        ]
    },
    {...}]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-06
      • 1970-01-01
      • 2021-02-15
      • 2020-01-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多