简体   繁体   English

我的数据库的每个表都需要一个类吗?

[英]do I need a class for every table for my db?

im trying to grasp LINQ as I understand it LINQ will be your DAL to your db which in turn creates for you automatically a class that maps out your db structure for you and then you can perform queries using that class .. 我试图掌握LINQ,因为我理解它LINQ将是您的数据库的DAL,它反过来为您创建一个类,为您映射您的数据库结构,然后您可以使用该类执行查询..

  • im trying to do a function that would return the result of my LINQ queries .. 我试图做一个将返回我的LINQ查询结果的函数..
    • all the examples i have been reading seems to point that if I want my queries to be a defined type I have to create a specific class for it and used it as List .. 我一直在阅读的所有示例似乎都指出,如果我希望我的查询是一个已定义的类型,我必须为它创建一个特定的类并将其用作List ..

am I correct in my assumption ?? 我的假设是正确的吗? please do enlighten me 请赐教

"Linq" is not an ORM technology, it is simply a way to write query like statements inside of .Net languages. “Linq”不是ORM技术,它只是一种在.Net语言中编写类似语句的查询的方法。 You have not said whether you are using Ling to SQL or Linq to Entities, which are both ORM technolgies that work with Linq to map your data schema to tables. 您还没有说过是使用Ling to SQL还是Linq to Entities,它们都是与Linq一起使用的ORM技术,可以将您的数据模式映射到表。

Linq to SQL will provide you with a straight mapping of every table to a class, and will handle most of the lifting to get this done. Linq to SQL将为您提供每个表到类的直接映射,并将处理大部分提升以完成此操作。 Your queries can return these types, or using your Linq expression you can "project" these types to other types that you have made yourself. 您的查询可以返回这些类型,或者使用您的Linq表达式,您可以将这些类型“投影”到您自己创建的其他类型。

If you need more flexibility, Entity Framework will allow you to map a single table to multiple entities, multiple tables to a single entity, etc. To do this you have to do a lot more work on your own, since you have to modify the mapping by hand in many cases. 如果您需要更多灵活性,Entity Framework将允许您将单个表映射到多个实体,将多个表映射到单个实体等。为此,您必须自己完成更多工作,因为您必须修改在许多情况下手工绘图。 Similar to Linq to SQL, queries can return either these generated types or specify their own types in query. 与Linq to SQL类似,查询可以返回这些生成的类型,也可以在查询中指定自己的类型。

By default, the Linq to SQL designer will create a class for each table in your database. 默认情况下,Linq to SQL设计器将为数据库中的每个表创建一个类。 The database context object that is created will have a definition for each table in your database. 创建的数据库上下文对象将为数据库中的每个表定义一个定义。 You can execute Linq queries against those table definitions and store the results in the generated classes, you have your queries generate anonymous types. 您可以对这些表定义执行Linq查询并将结果存储在生成的类中,您的查询将生成匿名类型。 It's up to you. 由你决定。

Based on the title of your question, i would say no. 根据你问题的标题,我会说不。 There could be instances that you have tables that you don't need to map to Linq2SQL. 可能存在您不需要映射到Linq2SQL的表的实例。 One immediate example that comes to mind, is if you were using the ASP.NET Membership API, you wouldn't need to drag those tables/sprocs/functions into the designer since that data is exposed by the API. 想到的一个直接示例是,如果您使用的是ASP.NET Membership API,则不需要将这些表/ sprocs /函数拖到设计器中,因为API会公开这些数据。

If you are using the designer(DBML file), you can pick and choose which db objects to bring into your solution. 如果您正在使用设计器(DBML文件),则可以选择要将哪些数据库对象引入解决方案。 This is the way that I have done it, only bringing in those db objects that I actually need access to. 这是我完成它的方式,只引入我实际需要访问的那些db对象。 As another example, I worked at a shop once that were strongly wed to the idea of all db access being done via stored procedures. 作为另一个例子,我曾经在一家商店工作过一次强烈要求通过存储过程完成所有数据库访问。 So in that case, I worked with a lot of [StoredProcedureName]Results which I in turn mapped to other objects, never the actual tables themselves, which werent even defined in the solution. 所以在这种情况下,我使用了很多[StoredProcedureName]结果,而这些结果又映射到其他对象,而不是实际的表本身,甚至在解决方案中也没有定义。

暂无
暂无

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

相关问题 如何在Fluent Mapping中处理没有数据库表的基类? - How do I deal with a base class with no db table in Fluent Mapping? 我需要在每个窗口中配置 Log4Net 还是有办法在基类中配置一次? - Do I need to configure Log4Net in every window or is there a way to do it once in a base class? 我是否需要在类构造函数中设置初始值? - Do I need to set initial values in my class constructor? 如果我的类实现了 IDisposable,为什么我需要终结器? - Why do I need a finalizer if my class implements IDisposable? 我需要在MVC5的ViewModel中实例化我的Model类吗? - Do I need to instantiate my Model class in a ViewModel in MVC5? 我是否需要使我的类变为静态才能使用静态方法? - Do I need to make my class static to use a static method? 如果我的Windows服务在ServiceAccount.LocalService下运行,我需要向谁授予数据库中的权限? - Who do I need to grant permissions to in my db if my Windows service is running under ServiceAccount.LocalService? 我想每分钟运行一个从我的 OnAppearing() 开始的方法。 我需要将此作为任务运行吗? - I want to run a method every minute that's started from my OnAppearing(). Do I need to run this as a task? 每次升级我的dotNet应用程序使用的COM库时,是否需要重建和重新部署? - Do I need to rebuild and redeploy every time I upgrade a COM library used from my dotNet app? 添加新的可空列后,是否需要更新某个表上的每个 C# sql 查询? - Do I need to update every C# sql query on a certain table after adding a new nullable column?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM