0

I am a newbie, i read MVC Movie App tutorial and created an AddressBook based on that, i am using ADO.NET Entity DataModel for stroing values in Database table, as i do not know anyother way, i am storing following values in my table- Id (primary key auto) name gender phoneno(Heres the problem i want to add multiple phone numbers, i want to add text boxes on clicking "add" link )

not only i want to add text boxes on view but also store it in database table , how do i do that? REMEMBER i am a newbie keep it simple

1 Answer 1

1

If you need more than one phone number for same Contact. It is a ONE to MANY relation ship. that means you need a seperate table to store your Phone numbers

I would create a new table called PhoneNumber like this structure

PHONE_NUMBER_ID (INT) PRIMARY KEY
PHONE_NUMBER (VARCHAR)
CONTACT_ID (INT) - Foreign key to the Contact table

Your sample data will look like

PHONE_NUMBER_ID     PHONE_NUMBER    CONTACT_ID
---------------     ------------     ----------
1                   734578956         1
2                   987546563         2
3                   987645643         2

This means Contact 1 has one phone number and contact 2 has 2 phone numbers.

Now you need a Collection property in your Contact class to store the PhoneNumbers

public class Contact
{
  public int ID { set;get;} 
  public string FirstName { set;get;}
  //Other contact related proerpties

  IList<string> PhoneNumbers { set;get;}

 public Contact()
 {
   if(PhoneNumbers==null)
       PhoneNumbers=new List<string>();
 }
}
Sign up to request clarification or add additional context in comments.

3 Comments

I do not have contact class in models , i just have edmx file in model (i know i am pathetic , just began learning.
how do i mention that phone number should be stored in Contact table while rest of the data should be stored in Table1
@user1557976 you should update your Data Access Code to do that. Without knowing how it is, it is difficult to tell you more specifically what to do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.