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

浪尽此生 提交于 2019-12-10 12:30:09

问题


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?


回答1:


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.




回答2:


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




回答3:


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

From:

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

To:

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



回答4:


What has not been mentioned in any of other answers is that the source of the problem in any case shown is the same: The "custom identification expression" passed in as a parameter of the AddOrUpdate method has to be a valid property of the entity being inserted or updated. Also, it 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 the AlbumPath was used and also note that the anonymous type requires no other fields to be created. Instead, you just need to specify the property names.

It worth mentioning to be careful when using AddOrUpdate as the result can be destructive.



来源:https://stackoverflow.com/questions/20169376/the-properties-expression-is-not-valid-the-expression-should-represent-a-proper

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!