0
using System.ComponentModel.DataAnnotations;

namespace exmapleNameSpace.DTO
{
    public class Edges
    {
        public long Id { get; set; }
        [Required]
        public string NameEdge;
        [Required]
        public Item Neighbor_ID1 { get; set; }
        [Required]
        public bool ID1isFile { get; set; }
        [Required]
        public Item Neighbor_ID2 { get; set; }
        [Required]
        public bool ID2isFile { get; set; }
    }
}

Items model

using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace exampleNameSpace.DTO
{
    public class Item
    {
        public long Id { get; set; }
        [Required]
        [StringLength(100)]
        public string Name { get; set; }
        [Required]
        public bool isFolder { get; set; }
        [Required]
        public bool isFile { get; set; }
        [RegularExpression(@"\w.([a - z]|[A-Z])")]
        [StringLength(100)]
        public string extension { get; set; }
        [Required]
        [StringLength(100)]
        public string parentPath { get; set; }
        [Required]
        public bool isEmpty { get; set; }
    }
}

I currently have my index built keeping ID as primary key in both the entities. I have Neighbor_ID1 and Neighbor_ID2 as two foreign keys, however I want to make a composite key consisting of (Neighbor_ID1, Neighbor_ID2) for uniquely identifying edges.

Can I just add [Key] annotations for both and edit my up method to have AddForeignKeyOperations() ?

I am quite new to EF and would like to know the advantages or disadvantages of the approach.

PS: I understand that cascade delete operations with my current approach wouldn't work. Assume a connected acyclic graph topology in between the said Edges and Verteces and that I would want a many-to-many relationship ideally.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
AvidJoe
  • 596
  • 5
  • 19
  • 2
    Does this answer your question? [Mapping composite keys using EF code first](https://stackoverflow.com/questions/19792295/mapping-composite-keys-using-ef-code-first) – Badro Niaimi Dec 13 '19 at 16:41
  • Have you considered using FluentApi? – Gleb Dec 13 '19 at 16:42
  • 1
    Why should the question be closed, when the duplicate pointed to ,doesn't answer the question completely? @marc_s – AvidJoe Dec 13 '19 at 21:35
  • The accepted answer clearly shows how to do this - why doesn't that answer your question?? – marc_s Dec 14 '19 at 06:57
  • "I am quite new to EF and would like to know the advantages or disadvantages of the approach. PS: I understand that cascade delete operations with my current approach wouldn't work. Assume a connected acyclic graph topology in between the said Edges and Verteces and that I would want a many-to-many relationship ideally" because i wanted to know what would happen in this scenario and if it would be a good idea in the first place @marc_s – AvidJoe Dec 14 '19 at 09:49

0 Answers0