简体   繁体   English

LINQ to SQL类实现接口

[英]LINQ to SQL Classes implementing an interface

I'm designing the data model for an app with the LINQ to SQL (O/R) designer and wonder: is it possible to have (many) classes implement an interface or base class and store the implemented fields' values in the DB? 我正在使用LINQ to SQL(O / R)设计器为应用程序设计数据模型,并且想知道:是否可以让(许多)类实现接口或基类,并将实现的字段的值存储在DB中?

Most if not all of my classes need common fields to be stored that describe metadata about the object, such as when and by whom the object was created, whether the object was deleted and when it was last modified... The values for these fields would ideally just go into columns on the implementing classes' tables in the database. 我的大多数类(如果不是全部)都需要存储用于描述有关对象的元数据的公共字段,例如创建对象的时间和对象,创建对象的对象是否被删除以及上次修改的时间...这些字段的值理想情况下,只需进入数据库中实现类表的列即可。

I know of the option to specify a global base class or interface in the .dbml file, but that's not very elegant since I will have some L2S classes that don't implement the interface. 我知道可以在.dbml文件中指定全局基类或接口的选项,但这并不是很好,因为我将有一些L2S类没有实现该接口。

I know that this is easy to do with something like NHibernate. 我知道使用NHibernate这样的东西很容易做到。 I'd just like to see if there is a way to do this with LINQ to SQL in an elegant way. 我只想看看是否有一种方法可以使用LINQ to SQL以一种优雅的方式做到这一点。

Any pointers? 有指针吗?

Yes, it is possible. 对的,这是可能的。 Just add separate code files with partial counterparts of your classes and implement your interfaces there. 只需添加单独的代码文件以及部分类,然后在其中实现您的接口即可。

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

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