views:

190

answers:

1

I'm using LINQ to model my database, but I'm writing it by hand (Steve Sanderson recommends this in his ASP.NET MVC book). What I need to know is what's happening when you create an EntityRef, and how it's referenced. If I create my queries manually (without using LINQ), but use LINQ to model it, and I bring back just the ID of something, then reference the actual table column using EntityRef in the view, does it do the join, or does it re-query the database for that bit of information?

To clear things up, if I have this in my repository:

public IEnumerable<MyTable> ListMyTable(int? myColumnVar)
{
  string query = "SELECT * FROM MyTable WHERE MyColumn = {0}";

  return this.ExecuteQuery<MyTable>(query, myColumnVar);
}

and then in my controller I do this:

IEnumerable<MyTable> mytables = _contractsControlService.ListMyTable(1);

return View(mytables);

then in my view I do things like

<%=tbl.Ref.MyColumn %>

I'm referencing something set out by the LINQ model, but isn't actually in the table output. How does it get that data?

To clear things up further, we're using systems which require ultimate speed, so the LINQ-to-SQL is too slow for us, hence why we're using direct queries in our repository. I wouldn't mind using this EntityRef business if only I knew what was happening underneath.

+1  A: 

You have most likely used the Object Relational designer to create an entity class for the Ref entity and an association between the MyTable and the Ref entity. You can also do that manually by creating the appropriate entity classes and using attributes to map the classes to the database schema. You can read the details in How to: Customize Entity Classes by Using the Code Editor (LINQ to SQL) on MSDN.

In your generated (or handwritten) code you will find some attributes:

[Table(Name="dbo.Ref")]
public partial class Ref : INotifyPropertyChanging, INotifyPropertyChanged

and

public partial class MyTable: INotifyPropertyChanging, INotifyPropertyChanged
{

  [Association(Name="Ref_MyTable", Storage="_Ref", ThisKey="RefId",
    OtherKey="Id", IsForeignKey=true)]
  public Ref Ref
  {
     get
     ...

The entity classes combined with the attributes enables the Linq-to-Sql framework to retrieve entity classes directly from the database.

If you want to monitor the SQL genereated by Linq-to-Sql you can assign the DataContext.Log property:

context.Log = Console.Out;

In your example, navigating from MyTable to Ref, probably generates SQL along these lines:

SELECT Id, Field1, Field2, Field3
FROM Ref
WHERE Id = @RefId
Martin Liversage
I forgot to mention that I _am_ writing the model by hand. I cannot rely on the auto-generated code. So referencing another field via my model creates a new SQL query and executes it against the SQL server?
Kezzer
@Kezzer: Yes, based on the attributes you have used to describe your object relational mapping. Use the DataContext.Log property to see the SQL used.
Martin Liversage
Okay, that's all I needed to know really, I hadn't realised that you could find out that way. Cheers.
Kezzer