C# viewmodel: 模型 -->无法进行类型转换工作

发布于 2024-10-19 15:20:06 字数 1024 浏览 2 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

十六岁半 2024-10-26 15:20:06

如果您从 EF 获取 People,则无法神奇地将其转换为 ViewPeople
仅当对象实际上 ViewPeople 时,您才可以将其转换为 ViewPeople

相反,您需要为 ViewPeople 提供一个构造函数,该构造函数接受 People 并复制其属性。

If you get a People from EF, you cannot magically convert it to a ViewPeople.
You can only cast an object to ViewPeople if it actually is a ViewPeople.

Instead, you need to give ViewPeople a constructor that takes a People and copies over its properties.

掌心的温暖 2024-10-26 15:20:06

当实体框架从数据库中获取的数据返回对象时,它会创建 People,而不是 ViewPeople。这解释了为什么你不能铸造你的物体。如果您想向 People 类添加功能,您可以将其添加到同一程序集中的另一个文件中:

public partial class People
{
    public String someFunction() {...} // additional functionality
}

您可以做的另一件事是让 ViewPeople 类在其构造函数中采用 People 实例,包装 People 实例的属性(哎呀!)并将您的查询更改为:

//select the person
NSDataContext dc = new NSDataContext(); // to get to the data context and models froM EF
ViewPeople person = (from p in dc.Peoples where p.id == personID select new ViewPeople(p)).First();

// pass model to view
return View(person)

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:

public partial class People
{
    public String someFunction() {...} // additional functionality
}

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:

//select the person
NSDataContext dc = new NSDataContext(); // to get to the data context and models froM EF
ViewPeople person = (from p in dc.Peoples where p.id == personID select new ViewPeople(p)).First();

// pass model to view
return View(person)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文