Auto-Implemented Properties related error

2 posts, 0 answers
  1. Siegmar
    Siegmar avatar
    2 posts
    Member since:
    Aug 2013

    Posted 04 Mar Link to this post

    Hi,
    This is the first time I am using Data Access and, considering the code below I get the following error message:

    System.MissingFieldException: There is no field with name 'artistId' backing 'ArtistId' property in type 'ZData.Chinook.Album'. You need to either change the Field Naming rules of the mapping configuration object or call HasFieldName(string) with the name of the backing field.

    As far as I understood from the link below, Data Access auto-implemented Properties:

    http://docs.telerik.com/data-access/developers-guide/code-only-mapping/mapping-clr-types-properties-and-associations/fluent-mapping-mapping-clr-field-name-strategies

    Any ideia how could I fix this error ?
    Thanks

    public partial class ChinookFluentMetadataSource : FluentMetadataSource
    {
        protected override IList<MappingConfiguration> PrepareMapping()
        {
            List<MappingConfiguration> configurations = new List<MappingConfiguration>();

            configurations.Add(AlbumMapping()); // <= THE PROBLEM IS HERE !!!
            configurations.Add(ArtistMapping());

            return configurations;
        }
    }

    public partial class ChinookFluentMetadataSource : FluentMetadataSource
    {
        private MappingConfiguration<Album> AlbumMapping()
        {
            MappingConfiguration<Album> mapping = new MappingConfiguration<Album>();

            mapping.MapType(x => new
            {
                AlbumId = x.AlbumId
                ,Title = x.Title
                ,ArtistId = x.ArtistId
            }).ToTable("Album");

            mapping.HasProperty(x => x.AlbumId)
                .HasColumnType("int")
                .IsIdentity(KeyGenerator.Autoinc)
                .IsNotNullable()
                .ToColumn("AlbumId");

            mapping.HasProperty(x => x.Title)
                .HasColumnType("varchar")
                .IsNotNullable()
                .HasLength(160)
                .ToColumn("Title");

            mapping.HasProperty(x => x.ArtistId)
                .HasColumnType("int")
                .IsNotNullable()
                .ToColumn("ArtistId");

            // Associations

            mapping.HasAssociation(x => x.Artist)
               .HasConstraint((x, y) => x.ArtistId == y.ArtistId)
               .WithOpposite(x => x.Albums);

            return mapping;
        }
    }

    public partial class ChinookFluentMetadataSource : FluentMetadataSource
    {
        private MappingConfiguration<Artist> ArtistMapping()
        {
            MappingConfiguration<Artist> mapping = new MappingConfiguration<Artist>();

            mapping.MapType(x => new
            {
                ArtistId = x.ArtistId
                ,Name = x.Name
            }).ToTable("Artist");

            mapping.HasProperty(x => x.ArtistId)
                .HasColumnType("int")
                .IsIdentity(KeyGenerator.Autoinc)
                .IsNotNullable();

            mapping.HasProperty(x => x.Name)
                .HasColumnType("varchar")
                .HasLength(120)
                .IsNullable();

            // Collections

            mapping.HasAssociation(x => x.Albums)
                .WithOpposite(x => x.Artist);

            return mapping;
        }
    }

    public partial class Album
    {
        public virtual int AlbumId { get; set; }
        public virtual string Title { get; set; }
        public virtual int ArtistId
        {
            get { return this.Artist == null ? ZLibraryDefaults.Default_Int32 : this.Artist.ArtistId; }
            set { this.Artist = new Artist(value); }
        }

        public virtual Artist Artist { get; set; } // ArtistId
    }

    public partial class Artist
    {
        public virtual int ArtistId { get; set; }
        public virtual string Name { get; set; }
        public virtual IList<Album> Albums { get; set; }
    }

  2. Siegmar
    Siegmar avatar
    2 posts
    Member since:
    Aug 2013

    Posted 05 Mar Link to this post

    Just to correct my former reply:

    "As far as I understood from the link below, Data Access SUPPORTS auto-implemented Properties:"

  3. DevCraft banner
Back to Top