简体   繁体   English

Linq-to-SQL:有多少datacontexts?

[英]Linq-to-SQL: how many datacontexts?

I have a SQL Server 2008 database with > 300 tables. 我有一个包含> 300个表的SQL Server 2008数据库。 The application I have to design is an Windows Forms app, .NET 3.5, C#. 我必须设计的应用程序是Windows Forms应用程序,.NET 3.5,C#。

Which is the best way to work with Linq-to-SQL ? 哪个是使用Linq-to-SQL的最佳方式?

I intend to make a datacontext for each business entity. 我打算为每个业务实体制作一个datacontext。

Is there any problem ? 有什么问题吗 ?

I need to know if this way of working with Linq-to-SQL has any disadvantage or can create performance issues ? 我需要知道这种使用Linq-to-SQL的方式是否有任何缺点或者是否会产生性能问题?

Thanks. 谢谢。

You should typically have 1 single DBML file (=data context) per database. 每个数据库通常应该有一个单独的DBML文件(=数据上下文)。 You should certainly not create a DataContext per business entity, because doing this would make you lose most of the useful capabilities of LINQ to SQL, like memory transactions (unit of work), lazy loading, and doing LINQ queries over multiple entities. 您当然不应该为每个业务实体创建一个DataContext ,因为这样做会使您失去LINQ to SQL的大部分有用功能,例如内存事务(工作单元),延迟加载以及对多个实体执行LINQ查询。

You have a pretty big model (+300 tables) which means a lot of entities. 你有一个非常大的模型(+300表),这意味着很多实体。 A lot of entities is not a big problem, except for the LINQ to SQL designer. 除了LINQ to SQL设计器之外,很多实体都不是一个大问题。 Using the designer with such big models can be pretty annoying. 使用具有如此大型号的设计师可能非常烦人。 This can be a reason to split a domain in multiple sub domains (with each a DBML file), but certainly not one per entity. 这可能是在多个子域中拆分域的原因(每个子域都有一个DBML文件),但肯定不是每个实体一个域。 However, keep in mind that you loose the L2S capabilities at the boundaries of the domains. 但是,请记住,您在域的边界处丢失了L2S功能。

In the past I advised a team, who had split up their +150 entities domain in 5 DBML files, to merge them back together to a single DBML. 在过去,我建议一个团队将5个DBML文件中的+150实体域分开,然后将它们合并为一个DBML。 The pain of editing the model went up, but the pain of using multiple DataContext s went away, which lowered the overall pain drastically for them. 编辑模型的痛苦上升了,但使用多个DataContext的痛苦消失了,这大大降低了他们的整体痛苦。

为每个业务实体创建数据上下文没有意义,每个数据库只需要一个datacontext。

well it depends on how many users will use your database simultaneously not how many tables are there. 这取决于有多少用户将同时使用您的数据库,而不是有多少表。 So its all about typical database issues: number of connections, locking and other stuff. 所以关于典型的数据库问题:连接数,锁定和其他东西。

I now use 1 for the entire database, but there are legitimate uses for having more. 我现在使用1作为整个数据库,但有更多的合法用途。 For example, I run a script when installing my site that connects to a remote DB and imports and converts data to the new format for deployment. 例如,我在安装连接到远程数据库的站点时运行脚本,并导入数据并将数据转换为新格式以进行部署。 The process uses some temporary tables. 该过程使用一些临时表。

By putting the temporary tables in a separate context, once the site is deployed I can simply delete these contexts and code as they are independent entities. 通过将临时表放在单独的上下文中,一旦部署了站点,我可以简单地删除这些上下文和代码,因为它们是独立的实体。

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

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