具有多对多关系的类上的CRUD操作示例实体框架核心

时间:2019-07-28 13:46:29

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

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

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还是自动创建?

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

1 个答案:

答案 0 :(得分:0)

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

Id  Title
1   Test
2   Test2
3   hello
4   world

创建新的Requires对象时,可以使用以下代码添加BlocksA

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"
        }
    ]
},
{...}]