none
Using RIA Services with EF Model First & DbContext RRS feed

  • Question

  • Hi,

    I have recently switched from NHibernate as my ORM to EF using DbContext.  I have chosen to use the database first approach (.edmx).  I have created my edmx file and selected DbContext as my code generator.  This has left me with classes like so:

    using System; using System.CodeDom.Compiler; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; [GeneratedCode("Microsofts Code", "")] [ExcludeFromCodeCoverage] public partial class Prescription { public Prescription() { this.PrescriptionForms = new HashSet<PrescriptionForm>(); } public long Id { get; set; } public long CreatedBy_id { get; set; }

    ...

    Since I am using RIA services every entity has to be decorated with a [Key].  I would have expected EF to do this for me, since it used to in the older versions like so:

    /// <summary>
        /// No Metadata Documentation available.
        /// </summary>
        [EdmEntityTypeAttribute(NamespaceName="Civica.DB.PrescribingModel", Name="Prescription")]
        [Serializable()]
        [DataContractAttribute(IsReference=true)]
        public partial class Prescription : EntityObject
        {
            #region Primitive Properties
        
            /// <summary>
            /// No Metadata Documentation available.
            /// </summary>
            [EdmScalarPropertyAttribute(EntityKeyProperty=true, IsNullable=false)]
            [DataMemberAttribute()]
            public global::System.Int64 Id
            {
                get
                {
                    return _Id;
                }
                set
                {
                    if (_Id != value)
                    {
                        OnIdChanging(value);
                        ReportPropertyChanging("Id");
                        _Id = StructuralObject.SetValidValue(value);
                        ReportPropertyChanged("Id");
                        OnIdChanged();
                    }
                }
            }
            private global::System.Int64 _Id;
            partial void OnIdChanging(global::System.Int64 value);
            partial void OnIdChanged();
    ...

    I guess its the EntityKeyProperty=true which is the part which tells RIA services that this is the key property.

    So my first question is, should this be done for me?  Or should I have to do it myself?  If so why isn't it working!?

    I assumed that I would have to do it myself, with DbContext is it still the correct approach to use partial classes and attribute the members there?  E.g.

        /// <summary>
        /// Partial class of prescription
        /// </summary>
        [MetadataType(typeof(Prescription.PrescriptionMetadata))]
        public partial class Prescription
        {
            /// <summary>
            /// The prescription metadata
            /// </summary>
            internal sealed class PrescriptionMetadata
            {
                /// <summary>
                /// Gets or sets the id.
                /// </summary>
                /// <value>
                /// The id.
                /// </value>
                [Key]
                public long Id { get; set; }

    Thanks

    Phil

    Wednesday, July 4, 2012 9:40 AM

Answers

  • Hi PhilDonegan,

    DbContext generator will not generate data annotation, because the mapping message and constraint are in .edmx file, so the generated classes are pure classes, without any additional message. If we want to add data annotation, we have to modify the T4 template. Here's an article about adding data annotation to t4 template, please refer here. For the second question, the classes is generated by T4 template, it generates partial classes is helpful if you want to add some customized to your classes. So, it isn't a problem.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by PhilDonegan Friday, July 6, 2012 7:22 AM
    Friday, July 6, 2012 3:25 AM
    Moderator