补充 ADO.net 实体数据模型中的实体
(使用 MVC 框架) 我创建了一个 ADO.net 实体数据模型。 现在我想用一些业务逻辑来扩展它,比如创建返回上下文子集的函数。 分部类是与数据模型一起创建的,因此我创建了一个新文件,声明如下所示的分部类:
我在其中放置了一个名为 GetMovieById(int id) 的函数,结果如下所示:
namespace RecordStore.Models
{
public partial class MovieDBEntities
{
public Movie GetMovieById(int id)
{
Movie movie = MovieSet.First(m => m.id == id);
return movie;
}
}
}
在开始之前我没有收到任何错误项目,然后错误是:
编译器错误消息:CS1061:“RecordStore.Models.MovieDBEntities”不包含“MovieSet”的定义,并且找不到接受“RecordStore.Models.MovieDBEntities”类型的第一个参数的扩展方法“MovieSet”(您缺少 using 指令或程序集引用吗?)
我不明白该错误,因为分部类的其他部分包含 MovieSet。 我的问题基本上是,如果这不是扩展数据模型的方法,那么方法是什么,如果是方法,为什么我会收到此错误?
(Playing around with the MVC framework)
I have created a ADO.net Entity Data Model. Now I'd like to extend it with some business logic, like creating functions returning subsets of the context. A partial class is created together with the Data Model, so I created a new file declaring a partial class like this:
I placed a function in it called GetMovieById(int id) and the result looks like this:
namespace RecordStore.Models
{
public partial class MovieDBEntities
{
public Movie GetMovieById(int id)
{
Movie movie = MovieSet.First(m => m.id == id);
return movie;
}
}
}
I get no errors until I start the project, and then the error is:
Compiler Error Message: CS1061: 'RecordStore.Models.MovieDBEntities' does not contain a definition for 'MovieSet' and no extension method 'MovieSet' accepting a first argument of type 'RecordStore.Models.MovieDBEntities' could be found (are you missing a using directive or an assembly reference?)
I don't understand the error because the other part of the partial class contains the MovieSet. My question is basically, if this is not the way to extend the Data Model, what would be the way, and in case it is the way, why am I getting this error?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我将把这是否是一项好技术的问题留给其他人。 但是,考虑到编译错误,您确定上面的部分类和模型生成的类位于相同的命名空间中吗?
I'll leave the question as to whether this is a good technique or not to someone else. Considering the compile error, however, are you sure your partial class above and the class generated by the model are in the same exact namespace?