0

I am currently stuck with one of my projects and need some help..

I created a table valued function in MSSQL that returns a table and a computed column (Distance) and have mapped it in Entity Framework 5 using the Entity Designer.

The problem is that I can only map the results either as a complex type or an entity that is in my model class. I need to use one of the Navigation Property in the entity and hence, I am stuck with using my entity class.

However, because the column Distance is not a real column in my table, I have no way of getting that value for every entity for the table valued function.

Is there any way of hacking around this? I've tried using a partial class but the function would not map the Distance column as a property for me.

Here's a little code to explain what I'm trying to do here :

IQueryable<Deal> deals = context.GetDealsByTypeLocationSearch(latitude, longitude);
foreach (var d in deals)
{
  Console.Write(d.Distance); 
}

The distance value is null when I tried to create a partial class for my entity Deal.

yulun
  • 260
  • 1
  • 6
  • 21

1 Answers1

0

Just to paraphrase the answer here, use the following property:

[DatabaseGenerated(DatabaseGeneratedOption.Computed)]

EDIT: Considering the use of a database first approach, have you looked at tweaking the DefiningQuery?

Community
  • 1
  • 1
Nick
  • 2,285
  • 2
  • 14
  • 26
  • I forgot to mention that I am using Database first, not Code first :( – yulun Nov 02 '12 at 10:38
  • how can i use this defining query with a stored procedure? – yulun Nov 02 '12 at 11:06
  • i tried it but i think if i were to use any navigation property based on my original entity , i would need to create the association in my ssdl too? – yulun Nov 02 '12 at 14:28
  • Ah, I think I may have misunderstood a little. Unfortunately, complex types do not support navigation properties. See here: http://msdn.microsoft.com/en-us/library/bb738472.aspx which is also mentioned here : http://stackoverflow.com/questions/5266092/ef4-complex-type-with-navigation-property-is-it-possible-or-alternatives – Nick Nov 02 '12 at 14:32