Entity Framework multiple navigation properties to the same table

ef-code-first entity-framework entity-framework-6

Question

I have a problem with EF Code First and I can't find how to solve it:

Let's say I have this two POCO:

public class DocumentRequest
{
    // Database Id
    public Guid Id { get; set; }

    // Origin
    public Guid OriginDocumentId { get; set; }
    public Document OriginDocument { get; set; }
    // Target
    public Guid TargetDocumentId { get; set; }
    public Document TargetDocument { get; set; }
}

public class Document
{
    // Database Id
    public Guid Id { get; set; }

    public string DocumentMessage {get; set;}

    public Guid? FromDocumentRequestId { get; set; }
    public DocumentRequest FromRequest { get; set; }

    public ICollection<DocumentRequest> SentDocumentRequests {get; set;}
}

When I create a DocumentRequest from one Document, I create a copy of the document and then a document request:

The original document will have a DocumentRequest added to the SentRequests navigation property

The copied document will have the FromDocumentRequestId pointing to the DocumentRequestId

How would you configure code first to do that?

1
0
8/6/2014 6:20:14 PM

Accepted Answer

If I understand you correctly, here is the classes and configuration you need.

Classes

public class Document
{
    public Guid Id { get; set; }
    public string DocumentMessage { get; set; }
    public DocumentRequest FromRequest { get; set; }
    public ICollection<DocumentRequest> SentDocumentRequests { get; set; }
}
public class DocumentRequest
{
    // One Document to one DocumentRequest
    [Key, ForeignKey("Document")]
    public Guid DocumentId { get; set; }
    public Document Document { get; set; }
    public Guid OriginDocumentId { get; set; }
    public Document OriginDocument { get; set; }
}

DbContext

public class AppContext : DbContext
{
    public DbSet<Document> Documents { get; set; }
    public DbSet<DocumentRequest> DocumentRequests { get; set; }
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        // One Document to many SendDocumentRequests.
        modelBuilder.Entity<Document>()
            .HasMany(x => x.SentDocumentRequests)
            .WithRequired(x => x.OriginDocument)
            .HasForeignKey(x => x.OriginDocumentId)
            .WillCascadeOnDelete(false);
    }
}

Structure

Structure

Sample Data

  • Document (Id = 1) is inserted
  • Document (Id = 1) is copied then Document (Id = 2) is inserted
  • Document Request is inserted
    • DocumentId = 2
    • OriginDocumentId = 1
  • Document (Id = 1) is copied again then Document (Id = 3) is inserted
  • Document Request is inserted
    • DocumentId = 3
    • OriginDocumentId = 1

Result

Document

Id
1
2
3

Document Request

DocumentId OriginDocumentId
    2            1
    3            1
1
8/6/2014 7:25:37 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow