简体   繁体   English

为什么不能在实体框架中继承模型的视图模型

[英]Why is not possible to a view model inherit from Model in entity framework

When I was developing an app in work, I got an error: 当我在工作中开发应用程序时,我收到一个错误:

Value cannot be null. Parameter name: input

There are a lots of post talking about that so I read them and... No solution. 很多帖子在谈论这个,所以我读了它们......没有解决方案。

Good to go on. 好继续。 So i checked if there are any property that is not nullable in database model that was trying to be insert and not, all of them where nullable(except the Identity) so that was not the problem. 所以我检查了在数据库模型中是否有任何不可为空的属性试图插入而不是,所有这些属性都可以为空(除了身份)所以这不是问题。 So I realized that the View Model where the error was being thrown was inhering from a Model class and I thought:" could that be the problem? ". 所以我意识到抛出错误的View Model 从Model类继承的 ,我想:“ 这可能是问题吗? ”。 so I am no longer inheriting from the model and get back to work. 所以我不再继承模型并重新开始工作。

Notice that this was not the first time that this happened me, two weeks ago I had the same situation and it was because of it. 请注意,这不是第一次发生这种情况,两周前我遇到了同样的情况而且是因为它。

Why was I inheriting from model class? 为什么我从模型类继承? Because it has +10 properties and I need it to display this values in view but I need also display values from another model class so I create a View Model: 因为它有+10属性,我需要它在视图中显示这些值,但我还需要显示另一个模型类的值,所以我创建了一个视图模型:

class ViewModel: ModelClass{

    //ModelClass contains +10 properties
    //and the current class contains 7 properties

    public ModelClass CastToModelClass()
    {
        return new ModelClass{ /*stuff...*/ }// casting the current properties values to the ModelClass class
    }
}

To when i insert the newly casted model is when i get the error: 当我插入新铸造的模型时,我得到错误:

public JsonResult Save(ViewModel model)
{
    var modelClass = model.CastToModelClass();
    // here there are not empty properties
    context.ModelClass.Add(modelClass);
    context.SaveChanges();// error throw
}

So my question is, why it is wrong to inherit from a Model class ? 所以我的问题是, 为什么从Model类继承是错误的

Your question "why is wrong inherit from a Model class?" 你的问题“为什么从Model类继承错误?” comes from the wrong assumption this issue is caused by inheritance . 来自错误的假设这个问题是由继承引起的 It is conceptually not advisable to do so, but technically it should not cause any issues when programmed correctly. 从概念上讲不建议这样做,但从技术上讲,正确编程时不应引起任何问题。

Others have responded already to why it is not a good idea to use entity framework models as (parts of) viewmodels, so I will address the technical issue at hand. 其他人已经回应了为什么使用实体框架模型作为视图模型的(部分)并不是一个好主意,因此我将解决手头的技术问题。

Given you do this: 鉴于你这样做:

var modelClass = model.CastToModelClass();
context.ModelClass.Add(modelClass);
context.SaveChanges();

And CastToModelClass() returns an EF model, makes your inheritance question irrelevant. CastToModelClass()返回一个EF模型,使你的继承问题无关紧要。 The problem must be in the code that prepares and returns a new ModelClass entity. 问题必须出在准备和返回新ModelClass实体的代码中。

If you do 如果你这样做

var entityModel = new ModelClass
{
    Prop1 = model.Prop1,
    Prop2 = model.Prop2,
    // ...
};
context.ModelClass.Add(entityModel);
context.SaveChanges();

You're basically doing the same. 你基本上也是这样做的。 That should just work; 这应该工作; if it doesn't, you're not properly mapping all properties. 如果没有,则表示您没有正确映射所有属性。 The problem does not lie within the inheritance. 问题不在于继承。

A model and a view model are two distinct concepts. modelview model是两个不同的概念。 Whereas a model relates to the business logic of your application, a view model is a direct adapter of this model to a view (thus, view model ). 模型与应用程序的业务逻辑相关,而视图模型是此模型对视图的直接适配器 (因此, 视图模型 )。

Furthermore, although they might share some properties and functionality, the view model will evolve based on the demands of what needs to be presented in the view; 此外,尽管它们可能共享一些属性和功能,但视图模型将根据需要在视图中呈现的内容的需求而发展; in some instance you will have multiple view models related to a single model. 在某些情况下,您将拥有与单个模型相关的多个视图模型。

In your example you are already trying to convert a view model to a model via, 在您的示例中,您已经尝试将视图模型转换为模型,

var modelClass = model.CastToModelClass();

That in itself is an assertion that you are dealing with two different concepts. 这本身就是一个断言,你正在处理两个不同的概念。 The pattern itself (returning a model class with the given properties set) is not bad; 模式本身(返回具有给定属性集的模型类)并不错; however, inheriting from the model class is completely unnecessary. 但是,继承模型类是完全没必要的。

class ViewModel : ModelClass // <-- Remove inheritance

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM