Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The properties expression is not valid. The expression should represent a property

I have these two entities

public class Song : IPathHavingEntity
    {
        public int Id { get; set; }
        [Required]
        public string Path { get; set; }
        [Required]
        public virtual Album Album { get; set; }
        [Required]
        public int TrackNumber { get; set; }
    }

public class Album : IPathHavingEntity
    {
        public int Id { get; set; }
        [Required]
        public string Path { get; set; }
        public virtual IEnumerable<Song> Songs { get; set; }
        [Required]
        public int AlbumNumber { get; set; }
    }

Path is defined in the IPathHavingEntity interface.

In my Seed method I want to add a song to the Songs table only if it doesn't exist. For this reason I check that the album path and song path combination don't exist already before adding it thus

context.Songs.AddOrUpdate(
    s => new { FilePath = s.Path, AlbumPath = s.Album.Path }, 
    new Song { TrackNumber = 1, Path = "01 Intro.mp3", Album = one });

The problem is I get this error

The properties expression 's => new <>f__AnonymousType0``2(FilePath = s.Path, AlbumPath = s.Album.Path)' is not valid. The expression should represent a property: C#: 't => t.MyProperty' VB.Net: 'Function(t) t.MyProperty'. When specifying multiple properties use an anonymous type: C#: 't => new { t.MyProperty1, t.MyProperty2 }' VB.Net: 'Function(t) New With { t.MyProperty1, t.MyProperty2 }'.

What's the problem?

like image 932
Sachin Kainth Avatar asked Nov 23 '13 23:11

Sachin Kainth


3 Answers

In my case, The Only modification I did that on the Model Classes forget to put {get; set;} with the property declaration, Thus ...It's solved my problem.

Like this:

Before:

 public int Supplier_ID;
 public String Supplier_Code;

After:

 public int Supplier_ID { get; set; }
 public String Supplier_Code { get; set; }

Kindly Check your Model Classes should have the Get/Set property

like image 166
user3278796 Avatar answered Nov 10 '22 16:11

user3278796


I struggled with a similar issue for several hours today and was finally able to resolve it. I'm not sure if this will work for your situation but it's worth investigating.

The problem may be caused by the Album property of your Song entity being marked as virtual. I'm not an EF expert but I don't think it likes that virtual property when initializing your anonymous type. Add a non-virtual property for the album path (but keep the virtual navigation property), like this:

public class Song : IPathHavingEntity
{
    public int Id { get; set; }

    [Required]
    public string Path { get; set; }

    [Required]
    public virtual Album Album { get; set; }

    public string AlbumPath { get; set; }

    [Required]
    public int TrackNumber { get; set; }
}

And then perform the AddOrUpdate using that non-virtual property, like this:

context.Songs.AddOrUpdate(
    s => new { FilePath = s.Path, AlbumPath = s.AlbumPath }, 
    new Song { TrackNumber = 1, Path = "01 Intro.mp3", Album = one });

EF should then only allow you to add songs where the given song path and album path do not already exist. Whether your Song domain object can have a non-virtual AlbumPath property is another question but this should at least allow you to run your seed method in the way you described.

like image 22
Troy Carlson Avatar answered Nov 10 '22 16:11

Troy Carlson


EF does not create a column for a property which does not have either getters or setters. For example, EF will not create columns for the following City and Age properties.

using System.ComponentModel.DataAnnotations;

public class Student
{
    private int _age = 0;

    public int StudentId { get; set; }
    public string StudentName { get; set; }
    public string City { get{ return StudentName;}  }
    public int Age { set{ _age = value;}  }
}

Referance : https://www.entityframeworktutorial.net/code-first/notmapped-dataannotations-attribute-in-code-first.aspx

like image 5
Kasun Wadasinghe Avatar answered Nov 10 '22 18:11

Kasun Wadasinghe