开发者

Simple question about LINQ to SQL

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开发者_Python百科 or does LINQ to SQL create 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜