locked
How to use annotation in edm from database? RRS feed

  • Question

  • User-1298832285 posted

    I have a database which i use to produce the Model classes (Model from the database). Since the file is auto generated I can't modify it, hence, I can't put the annotations, which in turn I can't put the DisplayName. 

    Any suggestion of how could I put the annotations would be greatly appreciated? Thanks a lot.

    //------------------------------------------------------------------------------
    // <auto-generated>
    //     This code was generated from a template.
    //
    //     Manual changes to this file may cause unexpected behavior in your application.
    //     Manual changes to this file will be overwritten if the code is regenerated.
    // </auto-generated>
    //------------------------------------------------------------------------------
    
    namespace PressureVessels.EDM
    {
        using System;
        using System.Collections.Generic;
        
        public partial class PrimaryInputTbl
        {
            [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
            public PrimaryInputTbl()
            {
                this.Jobs = new HashSet<Job>();
            }
        
            public int Id { get; set; }
            public Nullable<bool> IsVertical { get; set; }
            public Nullable<bool> IsHorizontal { get; set; }
            public Nullable<decimal> Pd { get; set; }
                @Html.LabelFor(model => model.Pd, htmlAttributes: new { @class = "control-label col-md-4" })
                <div class="col-md-2">
                    @Html.EditorFor(model => model.Pd, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.Pd)
                </div>

    For example, instead of Pd, I want to display, Design Pressure.

    Monday, March 21, 2016 2:20 PM

Answers

  • User-821857111 posted

    You can use partial classes and the Metadatatype attribute:

    [MetadataType(typeof(PrimaryInputTblMetaData))]
    public partial class PrimaryInputTbl
    {
    }
    
    public class PrimaryInputTblMetaData
    {
      [Display(Name="Design Pressure")]
      public Nullable<decimal> Pd { get; set; }
    }

    https://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.metadatatypeattribute(v=vs.110).aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, March 21, 2016 4:12 PM
  • User753101303 posted

    Hi,

    No, you won't put this in the generated files but in your own separate file. Because of the "partial" keyword,
    [MetadataType(typeof(PrimaryInputTblMetaData))]
    public partial class PrimaryInputTbl
    {
    }

    is part of the SAME class than the one defined in the generated file so as a result this attribute is applied to the generated class. And the metadata class itself defines all properties with the appropriate type. So you won't loose anything.

    Though unrelated it is still Worth to note that EF is moving away from EDMX files (no support in EF7) in favor of code based modelling (ie with "code first from database", you can directly generate the "C# code first" code for this existing db).

    Edit: if needed see https://msdn.microsoft.com/en-us/library/wa80x488.aspx about partial classes. Just make sure  also to use the same namespace (and class name of course) so that both declarations are combined.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, March 21, 2016 6:07 PM
  • User-821857111 posted

    This there a way around it,
    Yes - sorry,  I should have made that clear. As Patrice pointed out, you put that code in a separate file altogether.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, March 21, 2016 8:25 PM

All replies

  • User-821857111 posted

    You can use partial classes and the Metadatatype attribute:

    [MetadataType(typeof(PrimaryInputTblMetaData))]
    public partial class PrimaryInputTbl
    {
    }
    
    public class PrimaryInputTblMetaData
    {
      [Display(Name="Design Pressure")]
      public Nullable<decimal> Pd { get; set; }
    }

    https://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.metadatatypeattribute(v=vs.110).aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, March 21, 2016 4:12 PM
  • User-1298832285 posted

    [MetadataType(typeof(PrimaryInputTblMetaData))]

    The problem is that if I would update the database, for example, to add a new row (field), and then update the EDM from Database, then the Metadatatype on top of the partial class would disappear and I would have to put it there manual again. This there a way around it, so that whenever I update the edm from the database, it'll automatically add the metadatatype? Thanks a lot Mike.

    Monday, March 21, 2016 5:39 PM
  • User753101303 posted

    Hi,

    No, you won't put this in the generated files but in your own separate file. Because of the "partial" keyword,
    [MetadataType(typeof(PrimaryInputTblMetaData))]
    public partial class PrimaryInputTbl
    {
    }

    is part of the SAME class than the one defined in the generated file so as a result this attribute is applied to the generated class. And the metadata class itself defines all properties with the appropriate type. So you won't loose anything.

    Though unrelated it is still Worth to note that EF is moving away from EDMX files (no support in EF7) in favor of code based modelling (ie with "code first from database", you can directly generate the "C# code first" code for this existing db).

    Edit: if needed see https://msdn.microsoft.com/en-us/library/wa80x488.aspx about partial classes. Just make sure  also to use the same namespace (and class name of course) so that both declarations are combined.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, March 21, 2016 6:07 PM
  • User-821857111 posted

    This there a way around it,
    Yes - sorry,  I should have made that clear. As Patrice pointed out, you put that code in a separate file altogether.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, March 21, 2016 8:25 PM