Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The data objects don't track changes. The change tracking occurs on the DataContext and objects that you've retrieved through the DataContext. So in order to track changes you need to do the following:</p> <pre><code>public class FooDataContext : DataContext { public Table&lt;Order&gt; Orders; } public class Order { [DbColumn(Identity = true)] [Column(DbType = "Int NOT NULL IDENTITY", IsPrimaryKey = true, IsDbGenerated = true)] public int Id { get; set; } [DbColumn(Default = "(getutcdate())")] [Column(DbType = "DateTime", CanBeNull = false, IsDbGenerated = true)] public DateTime DateCreated { get; set; } [Column(DbType = "varchar(50)", CanBeNull = false, IsDbGenerated = false)] public string Name { get; set; } } </code></pre> <p>Now in your codebehind you can do something like:</p> <pre><code>public void UpdateOrder(int id, string name) { FooDataContext db = new FooDataContext(); Order order = db.Orders.Where(o=&gt;o.Id == id).FirstOrDefault(); if (order == null) return; order.Name = name; db.SubmitChanges(); } </code></pre> <p>I wouldn't recommend directly using the data context in the code behind, but this is a good way to get started with Linq To SQL. I would recommend putting all your database interactions in an external project and call from the GUI to the classes that encapsulate this behavior.</p> <p>I would recommend creating a Linq To Sql (dbml) file if you're new to Linq To Sql.</p> <p>Right click on your project in solution explorer, and select Add New Item. Select Linq To SQL file, and it will then let you connect to your database and select the tables.</p> <p>You can then look at the generated code, and get some great ideas on how Linq To Sql works and what you can do with it.</p> <p>Use that as a guideline on working with Linq to SQL and that will take you far...</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload