How to access the refrenced table fields in Ilist object of ASP.NET MVC
I have 2 tables, master, detail. 1.master table have fields (id, username, plan)-->id is primary key (PK) 2. detail table have fields (srNo,id, worksummary, ... )--> srNo is PK.
I have created foreign key relationship from detail to .master table for "id" field.
the code is:
IList<detail> objDetail=new List<detail> ();
IList<master> objMaster = new List<master> ();
string[] sarray = queryFields.Split('|');//
for (int i = 0; i < sarray.Length; i++)
{
string[] sfields = sarray[i].Split(',');
if (sfields[0] != "")
{
objDetail.Add(new _detail { Id = count , modify = sfields[1].ToString(), verified = sfields[2], I});
}
}
I have problem to add fields in "objDetail" using Add method. But I am unable to access the reference field "Id" , rest of the field of detail table can be accessed using objDetail.
How can I access the "Id" field from objDetail object to add in IList.
Update: Hi,
No, the queryField is not from database, it is just the hidden variable getting from the method parameter Count is integer variable in which I am assigning the value as the row number.
Type "detail" and " master " are the database tables. details type in line 1 is same as _detail (It was just spelling mistake). I am accessing the database using Entity frame work.
I am able to access all field of detail table but the field开发者_StackOverflow on which foreign key relation ship exist is not accessible.
How can I access that reference field.
thanks.
I am assuming you are using some sort of ORM. What you will likely want to do is not worry about the ID at all, but rather associate an entity relationship,
so let's presume your classes are defined as follows:
public class Master
{
public int Id { get; set; }
public string Username { get; set; }
public string Plan { get; set; }
public ICollection Details { get; set; }
}
public class Detail
{
public int srNo { get; set; }
public string Modify { get; set; }
public int MasterID { get; set; }
public Master Master { get; set; }
}
Then you associate them like this (assuming EF code first):
master.Details.Add(new Detail { Modify = "Something" });
context.SaveChanges();
Essentially you want to associate the detail to the master collection, the framework will take care of assigning an ID for you
精彩评论