自动向实体添加LinqToSql基类的方法? [英] Way to Automatically Add a LinqToSql base class to Entities?

查看:59
本文介绍了自动向实体添加LinqToSql基类的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否存在将基类添加到Linq2Sql实体的自动方法?

Is there an automatic way to add base classes to Linq2Sql entities?

我知道我可以定义一部分并实现,但是必须有一种自动化的方法,对吧?

I know I can define a partial and implement that but there has to be an automated way, right?

推荐答案

LINQ-to-SQL代码生成器直接支持此功能.

The LINQ-to-SQL code-generator supports this directly.

可以在设计器中将数据上下文的基类设置为Base Class属性.或者,直接编辑dbml:右键单击编辑方式...","XML编辑器"

The base-class for the data-context can be set in the designer, as the Base Class property. Alternatively, edit the dbml directly: right click, "Edit With...", "XML Editor"

要更改实体的基类,请设置类型:

To change the base class for the entities, set the type:

<Database EntityBase="Some.NameSpace.Foo" ... > 

要更改数据上下文的基类,请设置类型:

To change the base class for the data-context, set the type:

<Database BaseType="Some.NameSpace.Bar" ... >

在两种情况下,请在属性中使用完全限定的类型.等等.

In both cases, use the fully-qualified type in the attribute. Et voila.

请注意,因为它是非常文字的,所以您也可以使用这种方法使您的实体实现一个接口-例如,当我的类具有诸如LastUpdatedUpdatedBy之类的属性时,我可能会有一个IAuditable接口定义了这些.然后,我可以将代码放在数据上下文的SubmitChanges中(覆盖),该代码调用GetChangeSet()并为所有要更新的IAuditable实体设置这些值;非常甜美.

Note that because it is very literal, you can also use this approach to make your entities implement an interface - for example, when my classes have properties like LastUpdated and UpdatedBy, I might have an IAuditable interface that defines these. Then I can put code in my data-context's SubmitChanges (override) that calls GetChangeSet() and sets these values for all the IAuditable entities being updated; very sweet.

这篇关于自动向实体添加LinqToSql基类的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆