C# viewmodel: 模型 -->无法进行类型转换工作
我正在使用 EF 创建一个 linq-to-sql 模型,并且有一个运行良好的类,但是,我希望能够对类进行添加,并且在使用 EF 进行更改时不会覆盖它们,所以我相信我需要使用第二层视图类。
所以,我有:
public partial class People: INotifyPropertyChanging, INotifyPropertyChanged {...} // created by EF
然后想做:
public partial class ViewPeople: People {
public String someFunction() {...} // additional functionality
}
但是,当我尝试使用下面的代码从 People 转换为 ViewPeople 时,我得到一个异常:
Unable to cast object of type 'Namespace.Models.People' to 'Namespace.Models .ViewPeople'
代码:
//select the person
NSDataContext dc = new NSDataContext(); // to get to the data context and models froM EF
var person = (from p in dc.Peoples where p.id == personID select p).First();
// pass model to view
return View((ViewPeople)person)
如果我只是通过 person,我就可以访问所有“People”属性和方法。 当我修改 ViewPeople 类时,在引用“this”时,我可以访问超级“People”类的所有属性和方法。
当然,这很简单,但我就是不明白!
任何有关此事的帮助都将不胜感激,例如某人在自动生成的 EF 模型上实现视图模型的示例!
提前致谢, 安德鲁
I am creating a linq-to-sql model with EF, and have a class that works well, however, I want to be able to make additions to the class and not have them overwritten when I make changes with EF, so I believe I need to use a second layer of view class.
So, I have:
public partial class People: INotifyPropertyChanging, INotifyPropertyChanged {...} // created by EF
then want to do:
public partial class ViewPeople: People {
public String someFunction() {...} // additional functionality
}
however, when I try to cast from People to ViewPeople using the code further below, I get an exception:
Unable to cast object of type 'Namespace.Models.People' to 'Namespace.Models.ViewPeople'
Code:
//select the person
NSDataContext dc = new NSDataContext(); // to get to the data context and models froM EF
var person = (from p in dc.Peoples where p.id == personID select p).First();
// pass model to view
return View((ViewPeople)person)
If I just pass through person, I get access to all the "People" properties and methods.
When I modify the ViewPeople class, i get access to all properties and methods of the super "People" class when referencing 'this'.
Sure it's something simple, but I just can't figure it out!
Any assistance on the matter would be appreciated, as would an example to where someone has implemented viewmodels ontop of auto-generated EF models!
Thanks in advance,
Andrew
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您从 EF 获取
People
,则无法神奇地将其转换为ViewPeople
。仅当对象实际上是
ViewPeople
时,您才可以将其转换为ViewPeople
。相反,您需要为
ViewPeople
提供一个构造函数,该构造函数接受People
并复制其属性。If you get a
People
from EF, you cannot magically convert it to aViewPeople
.You can only cast an object to
ViewPeople
if it actually is aViewPeople
.Instead, you need to give
ViewPeople
a constructor that takes aPeople
and copies over its properties.当实体框架从数据库中获取的数据返回对象时,它会创建 People,而不是 ViewPeople。这解释了为什么你不能铸造你的物体。如果您想向 People 类添加功能,您可以将其添加到同一程序集中的另一个文件中:
您可以做的另一件事是让 ViewPeople 类在其构造函数中采用 People 实例,包装 People 实例的属性(哎呀!)并将您的查询更改为:
When the entity framework returns objects from data it fetches from the database, it creates People, not ViewPeople. That explains why you cannot cast your objects. If you want to add functionality to your People class, you can add this in another file in the same assembly:
Another thing you can do is make the ViewPeople class take a People instance in its constructor, wrap the People instance's properties (yikes!) and change your query to be: