This is a migrated thread and some comments may be shown as answers.

Auto-Implemented Properties related error

1 Answer 20 Views
Data Access Free Edition
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Siegmar
Top achievements
Rank 1
Siegmar asked on 05 Mar 2016, 12:30 AM

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; }
}

1 Answer, 1 is accepted

Sort by
0
Siegmar
Top achievements
Rank 1
answered on 05 Mar 2016, 03:10 PM

Just to correct my former reply:

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

Tags
Data Access Free Edition
Asked by
Siegmar
Top achievements
Rank 1
Answers by
Siegmar
Top achievements
Rank 1
Share this question
or