【发布时间】:2018-06-13 09:35:48
【问题描述】:
所以我从SO answer 部分遵循了如何在实体框架中存储具有数组数据类型的属性。我没有按照该答案将字符串 InternalData 设置为私有而不是公共,因为如果将其设置为公共,我发现它是一种代码味道(还没有足够的声誉来评论)。
我还设法从这个blog 映射实体框架中的私有属性。
当我从该实体执行 CR(创建、读取)时,一切顺利。但是,当我的 LINQ 查询有一个 where 子句使用该属性和数组数据类型时,它说 "System.NotSupportedException: '指定的类型成员在 LINQ to Entities 中不受支持。只有初始化程序、实体成员和实体导航支持属性。'"。
如何解决这个问题?以下是相关代码块:
public class ReminderSettings
{
[Key]
public string UserID { get; set; }
[Column("RemindForPaymentStatus")]
private string _remindForPaymentStatusCSV { get; set; }
private Status[] _remindForPaymentStatus;
[NotMapped]
public Status[] RemindForPaymentStatus
{
get
{
return Array.ConvertAll(_remindForPaymentStatusCSV.Split(','), e => (Status)Enum.Parse(typeof(Status), e));
}
set
{
_remindForPaymentStatus = value;
_remindForPaymentStatusCSV = String.Join(",", _remindForPaymentStatus.Select(x => x.ToString()).ToArray());
}
}
public static readonly Expression<Func<ReminderSettings, string>> RemindForPaymentStatusExpression = p => p._remindForPaymentStatusCSV;
}
public enum Status
{
NotPaid = 0,
PartiallyPaid = 1,
FullyPaid = 2,
Overpaid = 3
}
protected override void OnModelCreating(DbModelBuuilder modelBuilder)
{
modelBuilder.Entity<ReminderSettings>().Property(ReminderSettings.RemindForPaymentStatusExpression);
}
//This query will cause the error
public IEnumerable<ReminderSettings> GetReminderSettingsByPaymentStatus(Status[] statusArray)
{
var query = ApplicationDbContext.ReminderSettings.Where(x => x.RemindForPaymentStatus.Intersect(statusArray).Any());
return query.ToList(); //System.NotSupportedException: 'The specified type member 'RemindForPaymentStatus' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.'
}
【问题讨论】:
标签: entity-framework linq