Here is my question.I will try to make my question as simple as I can.. So, i have 2 classes and respectively two tables in a database which are not connected with each other, lets name them class A and class B wich look like:
public class User
{
[Key]
public int userID{ get; set; }
public User(string aName, string aPhone, string aEmail)
{
Name = aName;
Email= aEmail;
Phone= aPhone;
}
public string Name { get; set; }
public string Email{ get; set; }
public string Phone{ get; set; }
}
public class Work
{
[Key]
public int workID{ get; set; }
public Work(string aWorkName, string aAddress, string sPosition)
{
WorkName= aWorkName;
Address= aAddress;
Position= aPosition;
}
public string WorkName{ get; set; }
public string Address{ get; set; }
public string Position{ get; set; }
}
what I want to do now is to create third class, and third table in the database.I should be able to fill the columns in the third table with the information from the previous two tables. For example its columns should be Name Phone Email WorkName Address Position.So I take the NameID and workID and based on these primary keys I should fill the third table with all the information.How should it be done?I think it should be done something like this:
public class CombinedTable
{
[Key]
public int cID{ get; set; }
public User user{ get; set; }
public Work work{ get; set; }
}
and lets say in the main function some query like this:
var UserResult = from p in db.User
where p.UserId.Equals(userID)
select p;
var WorkResult= from p in db.Work
where p.WorkID.Equals(workID)
select p;
var combinedInfo= new CombinedTable()
{
User = UserResult,
Work= WorkResult
};
db.CombinedTable.Add(combinedInfo);
int savedChanges = db.SaveChanges();
But I am missing something.I hope you get the idea..I am using entity framework and code first.Any suggestions wiil be very appreciated.Thank you. P.S. : Just to clarify it one more time.If I take workID=5 and UserID= 1, in the third tabe there whould be created new row from the Information for user with ID 1 and work with ID 5.Do you get it?I hope someone get it..
Assuming you have a 1 to many relation between User and Work entities (One User can have many work)
That will create the tables with 1 to many relationships and you should be able to query it easily like this
EDIT: If you want a third table, create a class for that