c# - How does a strongly typed DataContext work? -


This is a deep continuation, which I'm still stumped about. I am using a strongly typed datacentext for my application and although it emits a warning, this magic works, how does it work?

It is usually used code to connect to a database using LINQ-to-SQL.

  class MyDatabase: DataContext {public table & lt; Widgets & gt; Widget; Public table & lt; Cars & gt; Cars; Public Myadabase (string connection): base (connection) {}}  

Although it emits a warning, it works out of the box. Anyone can start reading it using:

 using  (var db = new MyDatabase (connectionString)) {foreach (var in db.Widgets var) Console.WriteLine (w ); }   

widget looks like there is an area of ​​ MyDatabase class. And in my code, I'm not telling it anything. But elsewhere, I am reading it and why its value?

In other examples of LINQ-to-SQL, the data reference class with the code generated by the DBML layout tool of Visual Studio 2008 can look like this:

  public partial squares MyDatabase: Datacontex {public table & lt; Widgets & gt; Get Widgets {GetTable & lt; Widgets & gt; (); }}}  

Report to include partial and GetTable . Is partial required?

I am assuming my first example, eventually by calling GetTable , but then where is this code coming from? How do my data come with reference to the fields of data?

If they do not have the properties, then only the logical conclusion is that the base constructor has assigned them. Unexpected, perhaps, but not impossible.

partial allows you to combine multiple code files into a single code; You only need it if you is separated your code file (usually for designers).

In some definers, the reflector shows that ctor to private Zero InitTables (object schema)), which exactly (reflecting on the field, Assign them via GetTable (Type) ).


Comments

Popular posts from this blog

sql - dynamically varied number of conditions in the 'where' statement using LINQ -

asp.net mvc - Dynamically Generated Ajax.BeginForm -

Debug on symbian -