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

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

问题描述

这段代码不断出现这个错误。 :


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


我已经更改了几次,但它不断提出这个错误。

  using(SubscriberDBHandler db = new SubscriberDBHandler())
{
IEnumerable< Subscriber> NewSubscribers =订阅者
.Where(sub => db.Subscriber
.Any(aSub =>!aSub.Email.Equals(sub.Email)));
列表<订阅者> updateSubscribers = db.Subscriber
.Where(dbSub =>订阅者
.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))。状态??错误);
db.SaveChanges();
}

如果有人可以指出我的错误或想出一个更有效的方法来做到这一点。



提前感谢您的时间和帮助。






我知道有一些这个错误的帖子,但是在阅读它们时,我无法弄明白他们涉及到我的问题所以我很抱歉,如果这是一个常见的错误,而其他人提供了一个解决方案。



对象订阅者是一个列表< Subscriber>



我似乎找不到行,但是。堆栈跟踪确实包含这个。在System.Linq.Enumerable.ToList [TSource](IEnumerable 1来源)$ R

$ b



.SubScribRepository.AddOrUpdateSubscribers(List
1个订阅者)



解决方案

直接在LINQ语句中使用本地集合订阅者。但是这些对象不能被翻译成SQL。只有从原始类型到数据库类型的映射。



我建议您使用

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

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

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


This piece of code keep making this error. :

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

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.

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

解决方案

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.

I'd suggest you use

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

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天全站免登陆