属性表达式无效.表达式应表示一个属性 [英] The properties expression is not valid. The expression should represent a property
问题描述
我有这两个实体
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
是在IPathHavingEntity
界面中定义的.
在我的Seed方法中,我只想将歌曲添加到Songs
表中(如果它不存在).因此,在添加专辑路径和歌曲路径组合之前,我先检查该路径是否不存在
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 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 }'.
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 }'.
出什么问题了?
推荐答案
今天,我在一个类似的问题上苦苦挣扎了几个小时,终于得以解决.我不确定这是否适合您的情况,但值得调查.
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.
该问题可能是由Song
实体的Album
属性被标记为virtual
引起的.我不是EF专家,但是在初始化您的匿名类型时,我不认为它喜欢virtual
属性.为相册路径添加非虚拟属性(但保留virtual
导航属性),如下所示:
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; }
}
然后使用该非虚拟属性执行AddOrUpdate
,如下所示:
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应该只允许您添加给定的歌曲路径和专辑路径不存在的歌曲.您的Song
域对象是否可以具有非虚拟的AlbumPath
属性是另一个问题,但这至少应允许您以描述的方式运行种子方法.
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.
这篇关于属性表达式无效.表达式应表示一个属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!