I can’t understand one thing. Suppose I already have these coded entities:
[Table(Name = "Rates")]
public class Rate
{
private EntityRef<Product> _Product;
[Column(IsPrimaryKey = true)]
public String RateID
{ get; set; }
[Column]
public String ProductID
{ get; set; }
[Column]
public Double VolumeInTons
{ get; set; }
[Column]
public Decimal Volume
{ get; set; }
[Column]
public Decimal Cost
{ get; set; }
[Column]
public UInt32 Year
{ get; set; }
[Association(Storage = "_Product", OtherKey = "ProductID")]
public Product Product
{
get { return this._Product.Entity; }
set { this._Product.Entity = value; }
}
}
And
[Table(Name="Products")]
public class Product
{
private EntitySet<Rate> _Rates;
[Column(IsPrimaryKey= true)]
public String ProductID
{ get; set; }
[Column]
public String Name
{ get; set; }
[Column]
public String Category
{ get; set; }
[Association(Storage="_Rates", OtherKey="ProductID")]
public EntitySet<Rate> Rates
{
get { return this._Rates; }
set { this._Rates.Assign(value); }
}
}
- Should I have a physical database to connect to with the same table definitions (to use datacontext in my application)?
- Suppose I have created a database. Should it contain the same table definitions or does
LINQ to SQLcreate them?
If you already have a database, you connect to it from Visual Studio and drag-and-drop the tables into the design mode of your
DataContext. Visual Studio will then generate the corresponding classes for you.Alternatively, and probably the answer you are after, is that you already have the generated classes (for example: from a different system with the database), and you want to create the database according to your class definitions. You should then, somewhere early in your code, call once DataContext.CreateDatabase (as also noted by Reniuz), and the database will be created. Note that on your next run, you don’t need to create the database again.