What is DataContext:
Bridge that
allows execution of Linq query expression on actual database. Pass you Linq
expressions to DataContext to execute.
Simple LinqToSql Entity:
[Table]
public class Inventory
{
[Column]
public
string Make;
[Column]
public
string Color;
[Column]
public
string PetName;
// Identify
the primary key.
[Column(IsPrimaryKey
= true)]
public int
CarID;
public
override string ToString()
{
return
string.Format("ID = {0}; Make = {1}; Color = {2}; PetName
= {3}",
CarID,
Make.Trim(), Color.Trim(), PetName.Trim());
}
}
Getting Data From database:
DataContext
db = new DataContext(connectionString);
// Now
create a Table<> type.
Table<Inventory>
invTable = db.GetTable<Inventory>();
DataConext should be Strongly Typed:
class AutoLotDatabase
: DataContext
{
public
Table<Inventory> Inventory;
public
AutoLotDatabase(string connectionString)
:
base(connectionString){}
}
var bimmers = from s in db.Inventory where
s.Make == "BMW" select s;
Specifying Associations in Entity Classes:
[Table(Name="Customers")]
public partial class Customers :
INotifyPropertyChanging,
INotifyPropertyChanged
{
private
EntitySet<Orders> _Orders;
[Association(Name="FK_Orders_Customers",
Storage="_Orders",
OtherKey="CustID",
DeleteRule="NO ACTION")]
public
EntitySet<Orders> Orders
{
get; set; }
}
To generate the Entities directly from your database.
sqlmetal.exe /server(Specifies
the server hosting the database)
/database(Specifies the name of the
database to read metadata from)
/user(Specifies user ID to log in to
the server)
/password(Specifies password to log in
to the server)
/views(Generate code based on existing
database views)
/functions(Extract database functions)
/sprocs(Extract stored procedures
sqlmetal.exe Command-Line
/code(Output results as C# code or VB,
if you set the /language flag)
/language(Language used to defined the
generated types)
/namespace(Specifies the namespace to
define the generated types)