EntityFramework

  • Thread starter Thread starter gerry
  • Start date Start date
G

gerry

I have 2 tables associated through a 3rd table :

table1
ID - key
Value

table2
ID - key
Value

table3
ID1 - FK table1
ID2 - FK table2
Seq

Using the data model generated by the designer , how do I access
table2.Value :

using ( DBEntities dbe = new DBEntities() )
{
foreach ( Item1 t1item in dbe.Table1Items )
{
t1item .Table3Items.Load();
Console.WriteLine("{0} {1} {2}" , t1item.ID , t1item.Value ,
t1item.Table3Items.Count );
foreach ( Item3 t3item in i.Table3Items )
{
Console.WriteLine("\t{0} {1} {2} {3}" , t3item.ID1 , t3item.ID2 ,
t3item.Seq );

// *********************************
// t3Item.ID2 has a value and there is an existing item in table2
with that key value
// t3Item.Table2Item is always null & there is no t3Item.???.Load()
to load it.
//
Console.WriteLine("\t{0} {1} {2} {3}" , t3Item.Table2Item.Value );

}
}
}
 
Hi Gerry,

From your description, you're using ADO.NET Entity Framework to deal with
some relationed database tables. However, you found that some
NavigationProperties(geneated by the EDM tools in Visual studio) cannot
load records, correct?

I've setup 3 tables per your case and performed some local tests. My table
schema is exactly as what you have:

table1
table2 are pk tables

while table3 contains FK to both table 1 and table2

At first, I also got the problem you encountered. And as you said, there is
no "Load" method on the table3.table2Item or table3.table1item Navigation
property. However, I found that there is another property called
"[NavigationProperty]Reference". Therefore, for this case, in addition to
"table3.table1item" and "table3.table2item", there also contains the below
2 propeties:

table3.table1itemreference
table3.table2itemreference

And you can call "Load" method on these two properties so as to load the
relationed records. Here is the test code I used which can navigate from
table1 to table3 and to table2.


=================================
static void RunEDM()
{
EDMConsoleApp.testdb testdb1 = new testdb();




foreach (var t1item in testdb1.table1)
{
t1item.table3item.Load();

Console.WriteLine("id:{0}, name:{1}, t3itemcount:{2}",
t1item.t1_id, t1item.t1_name, t1item.table3item.Count);


foreach (var t3item in t1item.table3item)
{
t3item.table2itemReference.Load();
Console.WriteLine("t3_id{0}, t2_id{1}", t3item.t3_id,
t3item.table2item.t2_id);
}

}


}
=======================================

In addition, here are some web articles discussing about EDM and relations:

#Entity Data Model 101: Part 1
http://blogs.msdn.com/adonet/archive/2007/01/30/entity-data-model-part-1.asp
x

#Entity Data Model Associations: Where's my Foreign Key?
http://www.thedatafarm.com/blog/2007/09/11/EntityDataModelAssociationsWheres
MyForeignKey.aspx

Hope this helps.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.








--------------------
 
Thanks Steven - that did it.



"Steven Cheng" said:
Hi Gerry,

From your description, you're using ADO.NET Entity Framework to deal with
some relationed database tables. However, you found that some
NavigationProperties(geneated by the EDM tools in Visual studio) cannot
load records, correct?

I've setup 3 tables per your case and performed some local tests. My table
schema is exactly as what you have:

table1
table2 are pk tables

while table3 contains FK to both table 1 and table2

At first, I also got the problem you encountered. And as you said, there
is
no "Load" method on the table3.table2Item or table3.table1item Navigation
property. However, I found that there is another property called
"[NavigationProperty]Reference". Therefore, for this case, in addition to
"table3.table1item" and "table3.table2item", there also contains the below
2 propeties:

table3.table1itemreference
table3.table2itemreference

And you can call "Load" method on these two properties so as to load the
relationed records. Here is the test code I used which can navigate from
table1 to table3 and to table2.


=================================
static void RunEDM()
{
EDMConsoleApp.testdb testdb1 = new testdb();




foreach (var t1item in testdb1.table1)
{
t1item.table3item.Load();

Console.WriteLine("id:{0}, name:{1}, t3itemcount:{2}",
t1item.t1_id, t1item.t1_name, t1item.table3item.Count);


foreach (var t3item in t1item.table3item)
{
t3item.table2itemReference.Load();
Console.WriteLine("t3_id{0}, t2_id{1}", t3item.t3_id,
t3item.table2item.t2_id);
}

}


}
=======================================

In addition, here are some web articles discussing about EDM and
relations:

#Entity Data Model 101: Part 1
http://blogs.msdn.com/adonet/archive/2007/01/30/entity-data-model-part-1.asp
x

#Entity Data Model Associations: Where's my Foreign Key?
http://www.thedatafarm.com/blog/2007/09/11/EntityDataModelAssociationsWheres
MyForeignKey.aspx

Hope this helps.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support
Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no
rights.








--------------------
From: "gerry" <[email protected]>
Subject: EntityFramework
Date: Wed, 24 Dec 2008 12:31:00 -0500

I have 2 tables associated through a 3rd table :

table1
ID - key
Value

table2
ID - key
Value

table3
ID1 - FK table1
ID2 - FK table2
Seq

Using the data model generated by the designer , how do I access
table2.Value :

using ( DBEntities dbe = new DBEntities() )
{
foreach ( Item1 t1item in dbe.Table1Items )
{
t1item .Table3Items.Load();
Console.WriteLine("{0} {1} {2}" , t1item.ID , t1item.Value ,
t1item.Table3Items.Count );
foreach ( Item3 t3item in i.Table3Items )
{
Console.WriteLine("\t{0} {1} {2} {3}" , t3item.ID1 , t3item.ID2 ,
t3item.Seq );

// *********************************
// t3Item.ID2 has a value and there is an existing item in table2
with that key value
// t3Item.Table2Item is always null & there is no t3Item.???.Load()
to load it.
//
Console.WriteLine("\t{0} {1} {2} {3}" , t3Item.Table2Item.Value );

}
}
}
 
Thanks for your reply Germ,

I'm glad that it helps.

Have a nice day!

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).


--------------------
Subject: Re: EntityFramework
Date: Fri, 26 Dec 2008 15:58:28 -0500
Thanks Steven - that did it.



"Steven Cheng" said:
Hi Gerry,

From your description, you're using ADO.NET Entity Framework to deal with
some relationed database tables. However, you found that some
NavigationProperties(geneated by the EDM tools in Visual studio) cannot
load records, correct?

I've setup 3 tables per your case and performed some local tests. My table
schema is exactly as what you have:

table1
table2 are pk tables

while table3 contains FK to both table 1 and table2

At first, I also got the problem you encountered. And as you said, there
is
no "Load" method on the table3.table2Item or table3.table1item Navigation
property. However, I found that there is another property called
"[NavigationProperty]Reference". Therefore, for this case, in addition to
"table3.table1item" and "table3.table2item", there also contains the below
2 propeties:

table3.table1itemreference
table3.table2itemreference

And you can call "Load" method on these two properties so as to load the
relationed records. Here is the test code I used which can navigate from
table1 to table3 and to table2.


=================================
static void RunEDM()
{
EDMConsoleApp.testdb testdb1 = new testdb();




foreach (var t1item in testdb1.table1)
{
t1item.table3item.Load();

Console.WriteLine("id:{0}, name:{1}, t3itemcount:{2}",
t1item.t1_id, t1item.t1_name, t1item.table3item.Count);


foreach (var t3item in t1item.table3item)
{
t3item.table2itemReference.Load();
Console.WriteLine("t3_id{0}, t2_id{1}", t3item.t3_id,
t3item.table2item.t2_id);
}

}


}
=======================================
 
Back
Top