[asp.net-mvc]LINQ EF7 ASP.NET 和多对多

发布时间: 2017/3/25 3:30:41
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我如何可以将记录添加到我的数据库与关系在 ASP.NET MVC 6 多吗?

我试过这样,但它是错了

[HttpPost]
public IActionResult addpersons(Person pers)
{
    var ourdoc = db.Documents.FirstOrDefault(p => p.Documentid == docselected);
    ourdoc.DocPers.Add(ourdoc);
    db.SaveChanges();

    return Content("s");
}

我的模型文件的列表︰

DocContext.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Data.Entity;

namespace TDS_1_ASPNETMVC6.Models
{
    public class DocPer
    {
        public int Documentid { get; set; }
        public Document Document { get; set; }

        public int Personid { get; set; }
        public Person Person { get; set; }
    }

    public class DocContext : DbContext
    {
        public virtual DbSet<Document> Documents { get; set; }
        public virtual DbSet<Person> Persons { get; set; }


        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<DocPer>()
                .HasKey(t => new   
                {
                    t.Documentid,
                    t.Personid
                });


            modelBuilder.Entity<DocPer>()
                .HasOne(pt => pt.Document) 
                .WithMany(p => p.DocPers)
                .HasForeignKey(pt => pt.Documentid);

            modelBuilder.Entity<DocPer>()
                .HasOne(pt => pt.Person)
                .WithMany(t => t.DocPers)
                .HasForeignKey(pt => pt.Personid);
        }

    }

}

Documents.cs

using System.Collections.Generic;


namespace TDS_1_ASPNETMVC6.Models
{
    public class Document
    {
        public int Documentid { get; set; }
        public string Name { get; set; }
        public string Props { get; set; }


        public List<DocPer> DocPers { get; set; }
    }
}

Person.cs

using System.Collections.Generic;


namespace TDS_1_ASPNETMVC6.Models
{
    public class Person
    {
        public int Personid { get; set; }
        public string Iname { get; set; }
        public string Fname { get; set; }
        public string Oname { get; set; }
        public string Email { get; set; }



        public List<DocPer> DocPers { get; set; }

    }
}

解决方法 1:

首先,您的控制器不检查 null;如果 var ourdoc = db.Documents.FirstOrDefault(p => p.Documentid == docselected); 为空?所以 null 首先检查:)

此外 ourdoc.DocPers.Add(ourdoc); 必须将更改为ourdoc.DocPers.Add(new DocPer {Person = person});

第二,若要创建多对多,我建议您删除 DocPer 表并将其映射"后面",像这样︰

public class Document
{
    public Document()
    {
        Persons = new List<Person>();
    }

    public int Documentid { get; set; }
    public string Name { get; set; }
    public string Props { get; set; }


    public virtual ICollection<Person> Persons { get; set; }
}

public class Person
{
    public Person()
    {
        Documents = new List<Document>();
    }

    public int Personid { get; set; }
    public string Iname { get; set; }
    public string Fname { get; set; }
    public string Oname { get; set; }
    public string Email { get; set; }

    public virtual ICollection<Document> Documents { get; set; }

}

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Document>()
                .HasMany<Person>(s => s.Persons)
                .WithMany(c => c.Documents)
                .Map(cs =>
                {
                    cs.MapLeftKey("Documentid");
                    cs.MapRightKey("Personid");
                    cs.ToTable("DocPer");
                });
}

DocPer 表将由 EF 管理不能暴露。请检查网站为更多的细节。本指南将适用于 EF6 只,不 EF7 (它不支持多对多尚未,对于这种支持检查链接)。顺便说一句,EF7 仍然是不稳定,所以却记者︰ 可能会在将来,提供这种支持,但还没有人知道。

赞助商