How does foreign key work in C #? [closed]

1

I have two Contact (Man) and Woman tables. The man can have several women and a woman is for a man.

 public class Contato {
    public int id { get; set; }
    public String usuario { get; set; }
    public String nome { get; set; }
    public String senha { get; set; }
    public List<Mulher> listaMulher { get; set; }
}

  public class Mulher
  {
    public int idM { get; set; }
    public Contato id { get; set; }
    public String nome { get; set; }
    public String formacao { get; set; }

  }

Would it be this way? But what about the woman how can I add a key to the woman? How would that look in the main class?

    
asked by anonymous 21.11.2016 / 18:10

1 answer

1

In a one-to-many relationship

Data Annotations

 public class Student
    {
        public Student() { }

        public int StudentId { get; set; }
        public string StudentName { get; set; }   
        public int StdandardRefId { get; set; }           
        [ForeignKey("StandardRefId")]
        public virtual Standard Standard { get; set; }
    }

    public class Standard
    {
        public Standard()
        {
            StudentsList = new List<Student>();
        }
        public int StandardId { get; set; }
        public string Description { get; set; }

        public virtual ICollection<Student> Students { get; set; }
    }

in Fluent API

 protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
            //configure one-to-many
            modelBuilder.Entity<Standard>()
                        .HasMany<Student>(s => s.Students) Standard has many Students
                        .WithRequired(s => s.Standard)  Student require one Standard
                        .HasForeignKey(s => s.StdId);Student includes specified foreignkey property name for Standard
    }

source: link

    
21.11.2016 / 18:15