Entity Framework multiple navigation properties to the same table

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

Question

I'm having trouble with EF Code First and am unable to figure out how to fix it:

Say I possess these two POCOs:

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 make a document request from a single document, I first make a copy of the original document:

A DocumentRequest will be added to the original document's SentRequests navigation property.

The FromDocumentRequestId in the duplicated document will point to the DocumentRequestId.

How would you set up the code to do that first?

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

Accepted Answer

If I have understood you correctly, the classes and configuration you require are listed below.

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 Info

  • The document (Id = 1) is added.
  • After copying document ID 1, inserting document ID 2
  • A document request has been added.
    • FileId is 2
    • 0 OriginDocumentId
  • After copying document (Id = 1) once more, document (Id = 3) is inserted.
  • A document request has been added.
    • FileId is 3
    • 0 OriginDocumentId

Result

Document

Id
1
2
3

Information 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