How does foreign key in c#?

Asked

Viewed 90 times

1

I have two tables Contact(Man) and Woman. A 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 that be the way? But what about the woman how can I add a key to the woman? How would that look in the main class?

  • Just to confirm, you’re using EF, right?

  • What is an EF??????????

  • Entity Framework. If you’re not using it, you have to tell us what technology you intend to use.

  • I am not using and do not intend to use. Here at the moment I am only doing everything on hand same...

  • 1

    If you’re making one framework BD access yam It is up to you to define the API. Otherwise you will have to choose a framework existing. Then it depends on you to decide what best to watch out for, there are various options, EF, Nhibernate, Dapper, etc. Search and then add details to the question.

1 answer

1

In a Relationship One for Many

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:http://www.entityframeworktutorial.net/code-first/configure-one-to-many-relationship-in-code-first.aspx

  • That’s what it is? You’re using Entity framework?

  • yes Entity framework, but second option also serves for mapping Fluent Hibernate

  • But consider the question without the use of Entity framework pq here in the company they do not use.

  • thinking as object orientation is same thing only last part that does not exist

Browser other questions tagged

You are not signed in. Login or sign up in order to post.