Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Properties</strong> are not just syntactic sugar - they are important if you need to create object-relational mappings (<a href="http://weblogs.asp.net/scottgu/archive/2007/05/19/using-linq-to-sql-part-1.aspx" rel="nofollow">Linq2Sql</a> or <a href="http://weblogs.asp.net/scottgu/archive/2010/08/03/using-ef-code-first-with-an-existing-database.aspx" rel="nofollow">Linq2Entities</a>), because they behave just like variables while it is possible to hide the implementation details of the object-relational mapping (persistance). It is also possible to validate a value being assigned to it in the getter of the property and protect it against assigning unwanted values.</p> <p>You can't do this with the same elegance with methods. I think it is best to demonstrate this with a practical example.</p> <p>In one of his articles, Scott Gu creates classes which are mapped to the Northwind database using the "code first" approach. One short example taken from Scott's blog (with a little modification, the full article can be read at Scott Gu's blog <a href="http://weblogs.asp.net/scottgu/archive/2010/08/03/using-ef-code-first-with-an-existing-database.aspx" rel="nofollow">here</a>):</p> <pre><code>public class Product { [Key] public int ProductID { get; set; } public string ProductName { get; set; } public Decimal? UnitPrice { get; set; } public bool Discontinued { get; set; } public virtual Category category { get; set; } } // class Category omitted in this example public class Northwind : DbContext { public DbSet&lt;Product&gt; Products { get; set; } public DbSet&lt;Category&gt; Categories { get; set; } } </code></pre> <p>You can use entity sets <code>Products</code>, <code>Categories</code> and the related classes <code>Product</code> and <code>Category</code> just as if they were normal objects containing variables: You can read and write them and they behave just like normal variables. But you can also use them in Linq queries, persist them (store them in the database and retrieve them). <strong>Note</strong> also how easy it is to use <a href="http://blogs.msdn.com/b/efdesign/archive/2010/03/30/data-annotations-in-the-entity-framework-and-code-first.aspx" rel="nofollow">annotations</a> (C# attributes) to define the primary key (in this example <code>ProductID</code> is the primary key for <code>Product</code>).</p> <p>While the <strong>properties</strong> are used to define a representation of the data stored in the database, there are some <strong>methods</strong> defined in the <strong>entity set class</strong> which control the persistence: For example, the method <code>Remove()</code> marks a given entity as deleted, while <code>Add()</code> adds a given entity, <code>SaveChanges()</code> makes the changes permanent. You can consider the methods as actions (i.e. you control what you want to do with the data).</p> <p>Finally I give you an example how naturally you can use those classes:</p> <pre><code>// instantiate the database as object var nw = new NorthWind(); // select product var product = nw.Products.Single(p =&gt; p.ProductName == "Chai"); // 1. modify the price product.UnitPrice = 2.33M; // 2. store a new category var c = new Category(); c.Category = "Example category"; c.Description = "Show how to persist data"; nw.Categories.Add(c); // Save changes (1. and 2.) to the Northwind database nw.SaveChanges(); </code></pre>
    singulars
    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.
    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