locked
RIA services keyAttribute issue RRS feed

  • Question

  •  I've been reading countless posts about how to solve this issue I'm having with no luck.  I'm trying to call a stored procedure from a db and have it return some results.  I followed the basics (here's one link that describes what I've done)

    http://aspalliance.com/1925_ASPNET_40_and_the_Entity_Framework_4__Part_3__Execute_Stored_Procedures_Using_the_Entity_Framework_4.3

    Below is my service I created to extract the data (as you'll see I left in a bunch of commented code from trying different ways to get this to work and the bolded one is the one I think is correct).

    This all complies except that it's saying I need to have at least one key defined...yata yata...  So, I go and create a key and you know it....it still complains like it cant see the key I just specified.  You'll see I tried specifing the key after the "sealed" class and at the bottom (neither worked). 

     

    namespace SLfirstBind.Web.Services
    {
        using System;
        using System.Collections.Generic;
        using System.ComponentModel;
        using System.ComponentModel.DataAnnotations;
        using System.Data;
        using System.Linq;
        using System.ServiceModel.DomainServices.EntityFramework;
        using System.ServiceModel.DomainServices.Hosting;
        using System.ServiceModel.DomainServices.Server;
        using newmod;
    
    
        // Implements application logic using the cdrvault4Entities5 context.
        // TODO: Add your application logic to these methods or in additional methods.
        // TODO: Wire up authentication (Windows/ASP.NET Forms) and uncomment the following to disable anonymous access
        // Also consider adding roles to restrict access as appropriate.
        // [RequiresAuthentication]
        [EnableClientAccess()]
        public class getProc : LinqToEntitiesDomainService<cdrvault4Entities5>
        {
            
            // TODO:
            // Consider constraining the results of your query method.  If you need additional input you can
            // add parameters to this method or create additional query methods with different names.
            // To support paging you will need to add ordering to the 'QueryTrackers' query.
            public IQueryable<GetUserFavoriteReports_Result1> GetUserFavoriteReports(int UserID)
            {
                 Int32 vId = 5001;
               // return this.ObjectContext.GetUserFavoriteReports(Int32 UserID);
                //return this.ObjectContext.QueryTrackers;
                 //return ObjectContext.ExecuteFunction("GetUserFavoriteReports", new System.Data.Objects.ObjectParameter("UserID", UserID));
                 return ObjectContext.GetUserFavoriteReports(UserID).AsQueryable();
                // IQueryable<GetUserFavoriteReports_Result1> VGM = from GetUserFavoriteReports in ObjectContext.GetUserFavoriteReports(UserID)
                //                              // where GetUserFavoriteReports.UserID == vId
                //                               select ObjectContext.GetUserFavoriteReports(UserID);
                //return VGM;
            }
        }
    }

     

    //getProc.metadata.cs

    namespace newmod
    {
        using System;
        using System.Collections.Generic;
        using System.ComponentModel;
        using System.ComponentModel.DataAnnotations;
        using System.Linq;
        using System.ServiceModel.DomainServices.Hosting;
        using System.ServiceModel.DomainServices.Server;
    
    
        // The MetadataTypeAttribute identifies QueryTrackerMetadata as the class
        // that carries additional metadata for the QueryTracker class.
        [MetadataTypeAttribute(typeof(QueryTracker.QueryTrackerMetadata))]
        public partial class QueryTracker
        {
    
            // This class allows you to attach custom attributes to properties
            // of the QueryTracker class.
            //
            // For example, the following marks the Xyz property as a
            // required property and specifies the format for valid values:
            //    [Required]
            //    [RegularBLOCKED EXPRESSION]
            //    [StringLength(32)]
            //    public string Xyz { get; set; }
        
            internal sealed class QueryTrackerMetadata
            {
               // [Key]
                //public Int32 uID2;
     
                // Metadata classes are not meant to be instantiated.
                private QueryTrackerMetadata()
                {
                }
                            
                public string Archived { get; set; }
    
                public Nullable<DateTime> BeginDate { get; set; }
    
                public long QueryID { get; set; }
    
                public string QueryType { get; set; }
    
                [Key]
                public int UserID { get; set; }
            }
        }
    }


     

    Misc Info (in case it's relevant): 

    The stored proc references a table called "QueryTracker" with a primary key of "QueryID"

    I'm using VS2010 with what I think is the latest .dll as far as the RIA services are concered (silverlight - sys.compmodel.dataannotations runtime version - v2.0.50727 / Version 2.0.5.0 and on the web side version v4.0.30319 / Version 4.0.0.0).

    I reference the above item because I saw in other posts this was an issue.  I'm truly stumped and hope it's just some simple thing I'm missing or doing wrong, but I would appreciate anyones help in this matter.

    Thanks in advance

     

    Tuesday, August 24, 2010 6:04 PM

Answers

  • O.K...so I trashed the app and created a new one.  After playing around with it so much I had errors everywhere and became a nightmare.  I finally got this working after playing around and putting 10 different posts on how to do this together as each one had pieces of the puzzle, but none actually worked as they were all missing pieces that somehow your going to know how to do.  Anyways, so I went through all the basic steps once again and this time around I manually added in a class for my newly created "Service" that will be pulling the info from my stored proc.  This new service will be the "metadata.cs" portion that ISNT created for you like when you use a table.  Then in here I declared a "key" because it WILL complain and the other thing needed is at least ONE other field declaration or it wont work...my proc pulls back say 20 columns, but unless I declare at least one of these (which btw CANT be the key I declared) it wont work.  So I also declare another return set and tie the result into a datagrid and there is my data (well ONE row of data).  Moving on to figure out how to get multiple rows back now.

     

    Monday, August 30, 2010 3:48 PM

All replies

  •  Hi,

    Do you have a Key in GetUserFavoriteReports_Result1 class?

    Friday, August 27, 2010 2:03 AM
  • Frank thank you for the response!

    Do you mean declared in the following code of my  Entity Model where I imported the stored procedure?

    If so, I cant seem to add the [Key] attribute in anywhere...

    /// <summary>
        /// No Metadata Documentation available.
        /// </summary>
        [EdmComplexTypeAttribute(NamespaceName="newmod", Name="GetUserFavoriteReports_Result1")]
        [DataContractAttribute(IsReference=true)]
        [Serializable()]
        public partial class GetUserFavoriteReports_Result1 : ComplexObject
        {
           
            #region Factory Method
        
            /// <summary>
            /// Create a new GetUserFavoriteReports_Result1 object.
            /// </summary>
            /// <param name="reportTrackerID">Initial value of the ReportTrackerID property.</param>
            /// <param name="reportID">Initial value of the ReportID property.</param>
            /// <param name="reportName">Initial value of the ReportName property.</param>
            /// <param name="reportType">Initial value of the ReportType property.</param>
            /// <param name="userID">Initial value of the UserID property.</param>
            public static GetUserFavoriteReports_Result1 CreateGetUserFavoriteReports_Result1(global::System.Int64 reportTrackerID, global::System.Int32 reportID, global::System.String reportName, global::System.String reportType, global::System.Int32 userID)
            {
               
                GetUserFavoriteReports_Result1 getUserFavoriteReports_Result1 = new GetUserFavoriteReports_Result1();
               
                getUserFavoriteReports_Result1.ReportTrackerID = reportTrackerID;
                getUserFavoriteReports_Result1.ReportID = reportID;
                getUserFavoriteReports_Result1.ReportName = reportName;
                getUserFavoriteReports_Result1.ReportType = reportType;
                getUserFavoriteReports_Result1.UserID = userID;
                return getUserFavoriteReports_Result1;
            }
    
            #endregion
            #region Primitive Properties
       
            /// <summary>
            /// No Metadata Documentation available.
            /// </summary>
            [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)]
            [DataMemberAttribute()]
            public global::System.Int64 ReportTrackerID
            {
    
                get
                {
                    return _ReportTrackerID;
                }
                set
                {
                    OnReportTrackerIDChanging(value);
                    ReportPropertyChanging("ReportTrackerID");
                    _ReportTrackerID = StructuralObject.SetValidValue(value);
                    ReportPropertyChanged("ReportTrackerID");
                    OnReportTrackerIDChanged();
                }
            }

    ......etc.

    Monday, August 30, 2010 9:50 AM
  • O.K...so I trashed the app and created a new one.  After playing around with it so much I had errors everywhere and became a nightmare.  I finally got this working after playing around and putting 10 different posts on how to do this together as each one had pieces of the puzzle, but none actually worked as they were all missing pieces that somehow your going to know how to do.  Anyways, so I went through all the basic steps once again and this time around I manually added in a class for my newly created "Service" that will be pulling the info from my stored proc.  This new service will be the "metadata.cs" portion that ISNT created for you like when you use a table.  Then in here I declared a "key" because it WILL complain and the other thing needed is at least ONE other field declaration or it wont work...my proc pulls back say 20 columns, but unless I declare at least one of these (which btw CANT be the key I declared) it wont work.  So I also declare another return set and tie the result into a datagrid and there is my data (well ONE row of data).  Moving on to figure out how to get multiple rows back now.

     

    Monday, August 30, 2010 3:48 PM