关于LINQ to SQL的简单问题

发布于 2024-11-10 08:41:41 字数 1358 浏览 4 评论 0原文

我无法理解一件事。假设我已经拥有这些编码实体:

[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 创建它们?

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 SQL create them?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

往事随风而去 2024-11-17 08:41:41

如果您已有数据库,则可以从 Visual Studio 连接到该数据库,并将表拖放到 DataContext 的设计模式中。 Visual Studio 随后将为您生成相应的类。

或者,您想要的答案可能是您已经拥有生成的类(例如:来自具有数据库的不同系统),并且您希望根据您的类定义创建数据库。然后,您应该在代码早期的某个位置调用一次 DataContext.CreateDatabase(Reniuz 也指出),并且将创建数据库。请注意,在下次运行时,您不需要再次创建数据库。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文