错误:此上下文中仅支持原始类型或枚举类型 EF [英] Error: Only primitive types or enumeration types are supported in this context EF

查看:25
本文介绍了错误:此上下文中仅支持原始类型或枚举类型 EF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这段代码一直在犯这个错误.:

This piece of code keep making this error. :

无法创建类型为Repository.DBModel.Subscriber"的常量值.在此上下文中仅支持原始类型或枚举类型.

Unable to create a constant value of type 'Repository.DBModel.Subscriber'. Only primitive types or enumeration types are supported in this context.

我已经更改了几次,但它一直出现此错误.

I've Changed it a few times but it keeps coming up with this Error.

using (SubscriberDBHandler db = new SubscriberDBHandler())
{
    IEnumerable <Subscriber> NewSubscribers = Subscribers
                                              .Where(sub => db.Subscriber
                                              .Any(aSub => !aSub.Email.Equals(sub.Email)));
    List<Subscriber> updateSubscribers = db.Subscriber
                                           .Where(dbSub => Subscribers
                                           .Any(lSub => lSub.Email
                                           .Equals(dbSub.Email))).ToList();
    if(NewSubscribers.Count() >= 1)
    {
        db.Subscriber.AddRange(NewSubscribers);
    }
    updateSubscribers.ForEach(aSub => aSub.State = Subscribers
                                                  .FirstOrDefault(sub => sub.Email
                                                  .Equals(aSub.Email)).State ?? "Error" );
    db.SaveChanges();
}

如果有人能指出我的错误或想出更有效的方法来做到这一点,我将不胜感激.

I'd greatly appreciate if someone could point out my error or come up with a more efficient way to do this.

在此先感谢您的时间和帮助.

In advance thanks for your time and help.

我知道有一些帖子有这个错误,但是在阅读它们时,我无法弄清楚它们与我的问题有何关系.所以如果这是一个常见的错误并且其他人提供了解决方案,我很抱歉

I know there are a few post with this error out there but when reading them I can't figure out how they relate to my problem. so I'm sorry if this is a common mistake and others have provided a solution

对象 Subscribers 是一个 List

The object Subscribers is a List<Subscriber>

我似乎无法找到该行,但是.堆栈跟踪确实包含此内容.

I don't seem to be able to find the line but. the stack trace does contain this.

在 System.Linq.Enumerable.ToList[TSource](IEnumerable1 源)在 Repository.SubScribRepository.AddOrUpdateSubscribers(List1 Subscribers)

at System.Linq.Enumerable.ToList[TSource](IEnumerable1 source) at Repository.SubScribRepository.AddOrUpdateSubscribers(List1 Subscribers)

推荐答案

您可以直接在 LINQ 语句中使用本地集合 Subscribers.但是这些对象无法转换为 SQL.只有从原始类型到数据库类型的映射.

You use a local collection, Subscribers, directly in a LINQ statement. But these objects can't be translated into SQL. There are only mappings from primitive types to database types.

我建议你使用

var emails = Subscribers.Select(s => s.Email).ToList();

然后在 Contains 语句中使用这些字符串(即原始值),例如:

And proceed by using these strings (i.e. primitive values) in Contains statements like:

var newSubscribers = db.Subscriber
                       .Where(dbSub => !emails.Contains(dbSub.Email))
                       .ToList();
var updateSubscribers = db.Subscriber
                          .Where(dbSub => emails.Contains(dbSub.Email))
                          .ToList();

这篇关于错误:此上下文中仅支持原始类型或枚举类型 EF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆