简体   繁体   English

C#从DataContext表创建表?

[英]C# Create table from DataContext Table?

Is there a built in way to create a table if it does not exist from the DataContext Table class? 如果DataContext Table类中不存在表,是否有内置的方法可以创建表? Or is the only way to use raw sql statements? 还是使用原始sql语句的唯一方法?

If you are talking about using the built in linq-to-sql datacontext class then you can always refresh it from the database if you create a new table and it should be picked up. 如果您在谈论使用内置的linq-to-sql datacontext类,那么如果您创建一个新表并且应该将其选中,则始终可以从数据库中刷新它。

How about defining the table manually like this: 如何像这样手动定义表:

[Table(Name = "mydb.dbo.myTable")]
public class MyTable 
{
    [Column(Name = "Id", IsPrimaryKey = true)]
    public int Id { get; set; }

    [Column(Name = "ActiveDate")]
    public DateTime? ActiveDate { get; set; }

}

then you can access the table like this: 那么您可以像这样访问该表:

var table =  _seatingDataContext.Context.GetTable<MyTable>();

Then you can query it or add rows or whatever. 然后,您可以查询它或添加行或其他内容。 Hope that helps. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM