Powered by MSDN

US - English
NEW! Silverlight 5 is available Learn More

LinqToSqlDomainService and views RSS

16 replies

Last post Apr 29, 2009 06:32 AM by cconner101

(0)
  • cconner101

    cconner101

    Member

    7 Points

    76 Posts

    LinqToSqlDomainService and views

    Apr 28, 2009 11:16 AM | LINK

    I am trying to use a view in my DomainService and I keep on getting the error "The entity 'VwOrganizationUser' must have at least one property marked with the KeyAttribute."

    I have tried to use the mapping service to set the key attribute to true and have also tried to decorate the metadata class with the [key] attribute but to no avail.

     So what piece of documentation did I miss reading?

     

    Thanks

     

    Curtis Conner
    Ziios, Inc
  • kylemc

    kylemc

    Contributor

    7243 Points

    1147 Posts

    Microsoft

    Re: LinqToSqlDomainService and views

    Apr 28, 2009 01:36 PM | LINK

    Could you share the code where you declare the VwOrganizationUser and mark it with the Key attribute? It's difficult to get a good idea of the error without a sample.

    Kyle

  • ccchai

    ccchai

    Member

    194 Points

    76 Posts

    Re: LinqToSqlDomainService and views

    Apr 28, 2009 04:40 PM | LINK

    Try clean the solution then 'rebuild all' again. Sometimes the proxy classes at the client side are not generated correctly, I faced this problem several times already.


  • cconner101

    cconner101

    Member

    7 Points

    76 Posts

    Re: LinqToSqlDomainService and views

    Apr 29, 2009 01:02 AM | LINK

    OK so i think my confusion is as follows,  Does the key attribute go with the enity or with the declaration of the meta data?  We are using an existing ASP.NET application which uses Dynamic Data.  Our data layer is generated with the LinqToSql tool.  In the Table Object Diagram i have tried to set the key property to true but that does not decorate the class correctly. 

    I have tried to set the key value in the meta data as follows

    namespace ZiiosDAL
    {
        using System.ComponentModel.DataAnnotations;

        /// <summary>
        /// Construct to process Business Rules and custom properties for the VwOrganizationUser DAL calss
        /// /// </summary>
        [MetadataType(typeof(VwOrganizationUser_MD))]
        public partial class VwOrganizationUser
        {
            internal sealed class VwOrganizationUser_MD
            {
                [Key]
                [Required]
                public System.Guid UserGUID;

            }
        }
    }

    which this does not work also.  I must be missing something

    Curtis Conner
    Ziios, Inc
  • moravsky

    moravsky

    Participant

    1184 Points

    218 Posts

    Re: LinqToSqlDomainService and views

    Apr 29, 2009 02:05 AM | LINK

    You're definitely missing something. I'm willing to bet it's the namespace, your VwOrganizationUser class is probably not in the same namespace as your LinqToSql model. Anyways, I tried the following small example on my machine and it builds just fine as it should:

    namespace Ziios.Web
    {
        using System;
        using System.Collections.Generic;
        using System.ComponentModel;
        using System.ComponentModel.DataAnnotations;
        using System.Linq;
        using System.Web.Ria;
        using System.Web.Ria.Data;
        using System.Web.DomainServices;
       
        // TODO: Create methods containing your application logic.
        [EnableClientAccess()]
        public class ZiiosDomainService : DomainService
        {
            public IQueryable<VwOrganizationUser> GetVwOrganizationUser()
            {
                return null;
            }
        }

        [MetadataType(typeof(VwOrganizationUser_MD))]
        public partial class VwOrganizationUser
        {
            public Guid UserGUID { get; set; }
           
            internal sealed class VwOrganizationUser_MD
            {
                [Key]
                [Required]
                public Guid UserGUID;
            }
        }
    }

     
    --Petr aka pemoravs
  • cconner101

    cconner101

    Member

    7 Points

    76 Posts

    Re: Re: LinqToSqlDomainService and views

    Apr 29, 2009 02:26 AM | LINK

    Thanks for the reply.  The namespace is the same through out just to see what the issue is.  A couple of other things to think about

    1. Our data access is in a seperate dll that is built using the linqtosql tools.  So the table designer builds the starting entity as such

    namespace ZiiosDAL

    [Table(Name="dbo.vwOrganizationUser")]
     public partial class VwOrganizationUser
     {
      
      private System.Guid _UserGUID;
      
      public VwOrganizationUser()
      {
      }
      
      [Column(Storage="_UserGUID", DbType="UniqueIdentifier NOT NULL")]
      public System.Guid UserGUID
      {
       get
       {
        return this._UserGUID;
       }
       set
       {
        if ((this._UserGUID != value))
        {
         this._UserGUID = value;
        }
       }
      }
     

    I then implement the MetaData classes like so

    namespace ZiiosDAL

    [MetadataType(typeof(VwOrganizationUser_MD))]
        public partial class VwOrganizationUser
        {
            
            internal sealed class VwOrganizationUser_MD
            {
                [Key]
                [Required]
                public Guid UserGUID;
            }
        }

    and then i use the entity in the Ziios.Web namespace

    namespace Ziios.Web
    {
        using System;
        using System.Collections.Generic;
        using System.ComponentModel;
        using System.ComponentModel.DataAnnotations;
        using System.Linq;
        using System.Web.Ria;
        using System.Web.Ria.Data;
        using System.Web.DomainServices;

    using ZiiosDAL;
        
        // TODO: Create methods containing your application logic.
        [EnableClientAccess()]
        public class ZiiosDomainService : DomainService
        {
            public IQueryable
    <VwOrganizationUser> GetVwOrganizationUser()
            {
                return null;
            }
        }

    Notice that the Metadata is not in the Ziios.Web name space but in the ZiiosDAL namespace where the enity is declared is this the issue?


    Sorry for this being so involved but trying to reuse as much of the old code as possible

    Again, thanks for the replies

    Curtis Conner
    Ziios, Inc
  • moravsky

    moravsky

    Participant

    1184 Points

    218 Posts

    Re: Re: LinqToSqlDomainService and views

    Apr 29, 2009 02:49 AM | LINK

    Is VwOrganizationUser_MD in the same assembly as VwOrganizationUser?

    --Petr aka pemoravs
  • cconner101

    cconner101

    Member

    7 Points

    76 Posts

    Re: Re: Re: LinqToSqlDomainService and views

    Apr 29, 2009 02:51 AM | LINK

    Yes ZiiosDAL.dll in the namespace ZiiosDAL

    Curtis Conner
    Ziios, Inc
  • cconner101

    cconner101

    Member

    7 Points

    76 Posts

    Re: Re: Re: Re: LinqToSqlDomainService and views

    Apr 29, 2009 02:55 AM | LINK

    The only difference other than namespace that i can see between your example and mine is the declaration of

    Public System.Guid UserGUID;

     

    The declaration of mine is generated by the linqtosql designer and yours is in the actual class that also has the meta data declared.

     Know of any tool that i can use to look at the dll with to see if the attributes are set?

    Curtis Conner
    Ziios, Inc
  • moravsky

    moravsky

    Participant

    1184 Points

    218 Posts

    Re: Re: Re: Re: LinqToSqlDomainService and views

    Apr 29, 2009 03:01 AM | LINK

    Reflector is capable of this. You can download it at http://www.red-gate.com/products/reflector/.

    --Petr aka pemoravs