实体框架5 - 扩展DBContext类 [英] Entity Framework 5 - Extending DBContext Class

查看:169
本文介绍了实体框架5 - 扩展DBContext类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道我是否在这里做错事...



作为一个背景,我正在研究一个实体框架v5.0)解决方案,并且正在寻求向 DBContext 类添加额外的功能,所以任何tt生成的类(从DbContext继承)将具有这些内在功能自动。



根据答案我看到这里,我认为这将很简单,只需添加一个新的类,如下所示:

  Imports System.Data.Entity 
导入System.Data.Entity.Infrastructure

部分公共类DbContext
...添加我的方法/额外的东西在这里...
结束类

但是,当我创建这个类的时候,我的整个解决方案出现了与 DBContext不实现IDisposable ,或错误,如 sub OnModelCreating'无法声明为覆盖,因为它不会覆盖基类中的一个子。



基本上,如果我理解这个问题,在我创建这个时候,原来的DBContext似乎被忽略,我的解决方案假定这只是只有 DBContext类。



这将导致我相信DBContext在其定义中不是一个部分类(这会否定上述答案),但我也在想我知道太少了,可能只是做一些愚蠢的事情。



任何帮助/指导将非常感激!



另外,我知道这个示例代码是在VB.net中编写的,但我同样对c#/ VB.net解决方案感到满意。



谢谢!!

解决方案

同样,您正在自己的程序集中创建一个新类,名为 DbContext 。类型 DbContext (如果您没有使用完全限定名称)的所有成员(变量,属性等)现在将被映射到此类型。 p>

MSDN


您可以通过使用Partial关键字将多个声明中的类或结构的定义分开。您可以根据需要在尽可能多的不同源文件中使用尽可能多的部分声明。但是,所有的声明必须在同一个程序集和相同的命名空间中。


您的选项是:



1)子类:

 公共类DbContextEx 
继承DbContext
结束类

2)创建扩展方法:

 公共模块DbExtensions 

< Runtime.CompilerServices.Extension )>
公共函数Test1(source as DbContext)As Object
结束函数

< Runtime.CompilerServices.Extension()>
Public Sub Test2(source as DbContext)
End Sub

结束模块


I don't know if I'm doing something wrong here or not...

As a bit of background, I am working on an Entity Framework (v5.0) solution and was looking to add extra functionality to the DBContext class, so any tt-generated classes (that inherit from DbContext) will have that inherent functionality available to them automatically.

Based upon the answer I saw here, I figured it would be as easy as simply adding in a new class that would look as follows:

Imports System.Data.Entity
Imports System.Data.Entity.Infrastructure

Partial Public Class DbContext
    ... add in my methods / extra things here ...
End Class

But the moment I create this class, my entire solution comes up with errors related to things such as DBContext not implementing IDisposable, or errors such as sub 'OnModelCreating' cannot be declared 'Overrides' because it does not override a sub in a base class..

Basically, if I'm understanding the issue, the moment I create this, the original DBContext seems to be ignored and my solution assumes this is the only DBContext class.

That would lead me to believe that DBContext is not a partial class in its definition (which would negate the above-mentioned answer), but I'm also thinking I know too little and might just be doing something stupidly wrong.

Any help / guidance would be really appreciated!!

Also, I know this sample code was written in VB.net, but I'm equally comfortable with c# / VB.net solutions.

Thanks!!

解决方案

As is, you're creating a new class inside your own assembly called DbContext. All members (variables, properties, etc.) of type DbContext (if you haven't used a fully qualified name) will now be "mapped" to this type.

From MSDN:

You can divide the definition of a class or structure among several declarations by using the Partial keyword. You can use as many partial declarations as you want, in as many different source files as you want. However, all the declarations must be in the same assembly and the same namespace.

Your options are:

1) Subclass:

Public Class DbContextEx
    Inherits DbContext
End Class

2) Create extension methods:

Public Module DbExtensions

    <Runtime.CompilerServices.Extension()>
    Public Function Test1(source As DbContext) As Object
    End Function

    <Runtime.CompilerServices.Extension()>
    Public Sub Test2(source As DbContext)
    End Sub

End Module

这篇关于实体框架5 - 扩展DBContext类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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