EF代码优先许多一对多的关系工作 [英] EF Code First working with many-to-many relationships

查看:146
本文介绍了EF代码优先许多一对多的关系工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Visual Studio 2010,C#4.0和Entity Framework 5.0。我已经使用了很多年的数据库率先发展,但我试图移动代码第一,我遇到了问题。阅读和搜索似乎并没有解决问题。

I am using Visual Studio 2010, C# 4.0 and Entity Framework 5.0. I have been using database first development for many years but am trying to move to code first and am running into problems. Reading and searching does not seem to address the problems

我如下简化我的问题 - 我有两个班 - 评估师和文档

I have simplified my problem as follows - I have two classes - Assessors and Documents.

public class Assessor
{
    public int AssessorID { get; set; }
    public virtual List<Document> Documents { get; set; }       
}

public class Document
{
    public int DocumentID { get; set; }
    public string DocumentLocation { get; set; }
    public string DocumentName { get; set; }
    public virtual List<Assessor> Assessors { get; set; }
}



与上下文

with the context

public class DocumentAssignment : DbContext
{
    public DbSet<Assessor> Assessors { get; set; }
    public DbSet<Document> Documents { get; set; }
}

这是评估可以有许多文件和文档可以有许多评估(一经典的多对一对多的关系)。
我使用的约定来创建关系,但也用流利的API。我已经播种的文件表

An assessor can have many documents and a document can have many assessors (a classic many-to-many relationship). I am using convention to create the relationship but have also used the fluent API. I have seeded the document table.

我的两个问题:
ONE - 我要的文件分配给评审 - 什么是?最好的方法将其保存到数据库中。

My two questions: ONE - I want to assign documents to assessors - what is the best way to save this to the database?

两个我有以下方法来检索分配给评估文件:

TWO I have the following method to retrieve documents assigned to an assessor:

public static IEnumerable<MaternalDocument> GetAssignedDocumentList(int UserID, string ConnectionString)
    {
        using (DocumentAssignment dbContext = new DocumentAssignment(ConnectionString))
        {
            return returnValue = dbContext.MaternalAssessments
                .Where(m => m.AssessorID == UserID)
                .Include(m => m.MaternalDocuments)
                .Select(m => m.MaternalDocuments)
                .ToList();
        }
    }



但我不能得到这个,因为映射问题编译。我在做什么错了?

but I cannot get this to compile because of mapping issues. What am I doing wrong?

推荐答案

您必须告诉许多一对多的关系是如何建立的DbContext,在DocumentAssignment覆盖OnModelCreating。在此代码与您的关系表名称替换AssessorDocuments。

You have to tell the DbContext about how the many-to-many relationship is set up, by overriding OnModelCreating in DocumentAssignment. Replace AssessorDocuments in this code with your relation table name.

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Assessor>()
            .HasMany(a => a.Documents)
            .WithMany(d => d.Assessors)
            .Map(m =>
                {
                    m.MapLeftKey("AssessorID");
                    m.MapRightKey("DocumentID");
                    m.ToTable("AssessorDocuments");
                });
        base.OnModelCreating(modelBuilder);
    }

要分配文档到评估机构(假设的文稿1 DocumentID存在和评估师为1的AssessorID)存在:使用(VAR上下文=新DocumentAssignment())
{

To assign a Document to an Assessor (assuming a Document exists with DocumentID of 1 and an Assessor exists with an AssessorID of 1):

        using (var context = new DocumentAssignment())
        {
            var assessor = context.Assessors.Find(1);
            var document = context.Documents.Find(1);
            assessor.Documents.Add(document);
            context.SaveChanges();
        }

您GetAssignedDocumentList方法看起来是这样的:

Your GetAssignedDocumentList method would look something like this:

public static IEnumerable<Document> GetAssignedDocumentList(int UserID)
    {
        using (var context = new DocumentAssignment())
        {
            return context.Documents.Where(d => d.Assessors.Any(a => a.AssessorID == UserID));
        }
    }

这篇关于EF代码优先许多一对多的关系工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆