簡體   English   中英

關於 LINQ 到 SQL 的簡單問題

[英]Simple question about LINQ to SQL

我無法理解一件事。 假設我已經有這些編碼實體:

[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; }
    }
} 

[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); }
    }
}
  • 我應該有一個物理數據庫來連接相同的表定義(在我的應用程序中使用數據上下文)嗎?
  • 假設我已經創建了一個數據庫。 它應該包含相同的表定義還是LINQ to SQL創建它們?

如果您已經有一個數據庫,您可以從 Visual Studio 連接到它,然后將表拖放到DataContext的設計模式中。 然后 Visual Studio 將為您生成相應的類。

或者,您可能想要的答案是您已經擁有生成的類(例如:來自具有數據庫的不同系統),並且您希望根據 class 定義創建數據庫。 然后,您應該在代碼的早期某處調用一次DataContext.CreateDatabase (也如 Reniuz 所述),然后將創建數據庫。 請注意,在您下次運行時,您無需再次創建數據庫。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM