Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 关于LINQ到SQL的简单问题_C#_Database_Linq To Sql_Database Connection - Fatal编程技术网

C# 关于LINQ到SQL的简单问题

C# 关于LINQ到SQL的简单问题,c#,database,linq-to-sql,database-connection,C#,Database,Linq To Sql,Database Connection,有一件事我不明白。假设我已经有了这些编码实体: [Table(Name = "Rates")] public class Rate { private EntityRef<Product> _Product; [Column(IsPrimaryKey = true)] public String RateID { get; set; } [Column] public String ProductID { get; set

有一件事我不明白。假设我已经有了这些编码实体:

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

我是否应该连接一个物理数据库,并使用相同的表定义在我的应用程序中使用datacontext? 假设我已经创建了一个数据库。它应该包含相同的表定义,还是linqtosql创建它们?
如果您已经有一个数据库,那么可以从VisualStudio连接到它,并将表拖放到DataContext的设计模式中。然后VisualStudio将为您生成相应的类


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

请查看methodOk。我已经看到了实现这一目标的常规。所以我应该从DataContext继承,只使用其中所有表的组合?
[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); }
    }
}