简体   繁体   English

当我尝试将Metadatatype与viewmodel一起使用时,出现InvalidOperationException

[英]I get InvalidOperationException when I try to use Metadatatype with a viewmodel

I have a model with certain properties. 我有一个具有某些属性的模型。 I use a separated class with the MetadataType to define requirements for properties. 我将单独的类与MetadataType一起使用以定义属性要求。 I also try to use the same metadata class with a viewmodel defining just a subset of properties. 我还尝试将相同的元数据类与仅定义属性子集的viewmodel一起使用。 This is a simple example code of the situation for clarification: 这是用于说明情况的简单示例代码:

[MetadataType(typeof(Metadata))]
class ModelA
{

    public class Metadata
    {
         [Required]
         public object Property1 { get; set; }

         [Required]
         public object Property2 { get; set; }

    }

    public int Property1 { get; set; }
    public int Property2 { get; set; }
}

[MetadataType(typeof(ModelA.Metadata))]
class ViewModelA
{
    public int Property1 { get; set; }
}

The problem is when the razor engine tries to process the view, it throws the an InvalidOperationException with the following error message: 问题是剃刀引擎尝试处理视图时,它会引发InvalidOperationException并显示以下错误消息:

The associated metadata type for type ' ViewModelA ' contains the following unknown properties or fields: Property2 . 类型' ViewModelA '的关联元数据类型包含以下未知属性或字段: Property2 Please make sure that the names of these members match the names of the properties on the main type. 请确保这些成员的名称与主类型上的属性的名称匹配。

In my understanding the problem here is that the metadata contains properties which the the view model does not. 以我的理解,这里的问题是元数据包含视图模型不包含的属性。 However, this way I don't really understand the advantages of having a metadata class. 但是,以这种方式,我并不真正了解拥有元数据类的优点。 So my questions are 所以我的问题是

1) Is there a way to prevent of throwing this exception? 1)有没有办法防止引发此异常?

2) If not, what it the best pattern to this situation? 2)如果不是,那么对这种情况最好的模式是什么? (like using a model, a viewmodel, which contains the subset of the properties of the model, defining data annotations by keeping the DRY approach). (就像使用模型一样,视图模型包含模型属性的子集,通过保留DRY方法定义数据注释)。

MetadataTypeAttribute's are typically used in cases where you want to apply Metadata to auto-generated objects. MetadataTypeAttribute通常用于要将元数据应用于自动生成的对象的情况。 If you were to apply the metadata to the objects themselves, they would be overwritten when the objects were regenerated. 如果将元数据应用于对象本身,则在重新生成对象时,它们将被覆盖。

This is typically done via partial classes. 通常通过部分类完成此操作。 For instance see below. 例如,见下文。 In this case, you create an empty partial class that is the same name as the auto-generated class, it's of course named the same, but you add the MetadataType attribute to it, and you include the nested Metadata class within it. 在这种情况下,您将创建一个空的部分类,该部分类与自动生成的类同名,当然它的名称也相同,但是您要向其中添加MetadataType属性,并在其中包含嵌套的Metadata类。

You do not typically use this with other classes, as other classes would have to have identical (or supersets) of properties. 您通常不会将其与其他类一起使用,因为其他类必须具有相同的(或超集)属性。

// Do no edit this class as it is auto generated
public partial class ModelA
{
    public int Property1 { get; set; }
    public int Property2 { get; set; }
}

// This class can be edited
[MetadataType(typeof(ModelA.Metadata))]
public partial class ModelA
{
    public class Metadata
    {
         [Required]
         public object Property1 { get; set; }

         [Required]
         public object Property2 { get; set; }

    }
}

So, to answer your questions: 因此,回答您的问题:

1) No, you can't prevent the exception, because Metadata has to describe all the properties of it's parent class, and if those properties don't exist, it throws. 1)不,您不能阻止该异常,因为元数据必须描述其父类的所有属性,如果这些属性不存在,则会引发异常。

2) You would have to create alternate metadata classes. 2)您必须创建备用元数据类。 Since this is just as much work as creating a stand-alone view-model, there is no real benefit to using buddy classes for view models that are subsets. 由于这与创建独立视图模型一样多,因此将伙伴类用于子集的视图模型并没有真正的好处。

A lot of people get caught up in the DRY concept. 很多人陷入DRY概念。 The problem is that every software implementation has competing requirements. 问题在于每个软件实现都有相互竞争的要求。 DRY is often at odds with Single Responsibility Principle. DRY通常与单一责任原则不符。

MetadataTypeAttribute is really more of a hack anyways to deal with the way that the designers generate code. 无论如何,MetadataTypeAttribute实际上更像是一种黑客,可以用来处理设计人员生成代码的方式。 If you're using Code First, then I would strongly discourage using MetadataTypeAttribute. 如果您使用的是Code First,那么我强烈不建议使用MetadataTypeAttribute。

暂无
暂无

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

相关问题 当我尝试使用Web API 2的属性路由时,为什么会出现InvalidOperationException? - Why do I get an InvalidOperationException when I try to use attribute routing with Web API 2? 当我运行时,尝试写出XML文件时会收到InvalidOperationException。 - When I run I get an InvalidOperationException when I try to write out the XML file 将视图模型发送到局部视图时,为什么会出现类型错误 (InvalidOperationException)? - Why do I get a type error (InvalidOperationException) when sending a viewmodel to a partial view? 如何以编程方式添加更多样式设置器? 当我尝试时,出现InvalidOperationException异常(正在使用SetterBaseCollection) - How to add further style setters programmatically? When I try I got an InvalidOperationException exception (SetterBaseCollection is in use) 为什么在尝试渲染此.NET视图时会获得此“ InvalidOperationException”? - Why I obtain this “InvalidOperationException” when I try to render this .NET view? 当我尝试从WPF中的RichTextBox手动删除图像时出现InvalidOperationException - InvalidOperationException when I try to delete an image manually from RichTextBox in WPF 调用服务时出现InvalidOperationException - I get an InvalidOperationException When Invoking a Service 为什么我在尝试使用 AutoMapper 时会出现异常? - Why I get exception when I try to use AutoMapper? 尝试使用“ CopyEx”时出现“无效名称”错误 - I get an “Invalid Name” error when I try to use “CopyEx” 当我尝试从数据库中检索密码时,发生InvalidOperationException,“如果不存在任何数据,则尝试进行无效读取。” - InvalidOperationException when i try to retrieve a password from database, “Invalid attempt to read when no data is present.”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM