0
votes

I let VS create my model from existing SQL tables using EF 4.3.1. Because the DB tables are not final and may change I don't want to set the validation attributes directly into the model file because it gets overwritten when I update the model.

I want to use the MetadataTypeAttribute approach described here: Update Model From Database (Database First)

So I created an external file containing this class:

using System.ComponentModel.DataAnnotations;

namespace PDB.Models
{
    [MetadataTypeAttribute(typeof(t_scriptingMetadata))]
    public partial class t_scripting
    {
    }

    public class t_scriptingMetadata
    {
        [Required]
        public int platform {get; set;}

        [Required]
        [StringLength(20)]
        public string Projectname {get; set;}
    }
}

Unfortunately I get these errors in VS:

  • Duplicate EdmEntityTypeAttribute attribute
  • Duplicate Serializable attribute
  • Duplicate DataContractAttribute attribute

My model that has been generated by VS has exactly these attributes the error message mentions:

[EdmEntityTypeAttribute(NamespaceName="CAWI_STDMGTModel", Name="t_scripting")]
[Serializable()]
[DataContractAttribute(IsReference=true)]
public partial class t_scripting : EntityObject
{
    //...
}

What am I doing wrong?

2

2 Answers

0
votes

Your t_scriptingMetadata class have to be into t_scripting not necessarily

    [MetadataTypeAttribute(typeof(t_scriptingMetadata))] 
public partial class t_scripting 
{ 
  public class t_scriptingMetadata 
  { 
     [Required] 
     public int platform {get; set;} 

     [Required] 
     [StringLength(20)] 
     public string Projectname {get; set;} 
  } 
} 

And both partial classes must have same namespace. Hope this help. Regards

0
votes

I have found the problem. I had a table in my model that actually is a view containing fields from another table that was also in my model. So the error messages were indeed right. Thank for your help, vfabre!