Expression of properties is not allowed. The expression must represent a property

I have these two objects

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 defined in the IPathHavingEntity interface.

In the Seed method, I want to add a song to the Songs table only if it does not exist. For this reason, I check that the path of the path to the album and the combination of the path of the song do not exist before it is added.

 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 that I get this error

The properties expression => 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 is the problem?

+15
entity-framework-6
source share
4 answers

I struggled with a similar problem for several hours today and finally was able to solve it. I'm not sure if this will work for your situation, but worth exploring.

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

 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 execute AddOrUpdate using this non-virtual property, for example:

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

EF should allow you to add songs in which a given song path and album path does not yet exist. If your Song domain object may have a non-virtual AlbumPath property, this is another question, but that should at least allow you to run your seed method in the form you described.

+4
source share

In my case, "The only modification I made was that in class classes I forgot to put {get; set;} with the property declaration, that way ... He 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; } 

Remember to verify that your model classes must have the Get/Set property Get/Set

+33
source share

In my case, changing the following values ​​in the mapper worked.

From:

 this.HasKey(t => new { FirstName = t.FirstName, LastName = t.LastName }); 

In order to:

 this.HasKey(t => new { t.FirstName, t.LastName }); 
0
source share

No other answer mentioned that the source of the problem in any case is the same: the "user identification expression" passed as a parameter to the AddOrUpdate method must be a valid property of the object that is being inserted or updated. In addition, he will not accept ComplextType.Property there.

For example:

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

Note that the problem was resolved when using AlbumPath, and also note that the anonymous type does not require the creation of other fields. Instead, you just need to specify property names.

It is worth noting that you should be careful when using AddOrUpdate, as the result can be devastating .

0
source share

All Articles