无法施放类型的对象' collections.generic.list`1 [microsoft.entityframeworkcore.query.iincludablequeryable`2
我正在尝试从查询中添加类的属性。我有两个类别的“文章”,以及与Manuttomany关系的来源。 资料来源:
public class Source
{
public Source()
{
this.Id = Guid.NewGuid();
}
public Guid Id { get; set; }
public int ApiId { get; set; }
public string? FullName { get; set; }
public string? Bio { get; set; }
public virtual ICollection<Article> Articles { get; set; }
}
文章:
public class Article
{
public Article()
{
this.Id = Guid.NewGuid();
}
public Guid Id { get; set; }
public int ApiId { get; set; }
public string Title { get; set; }
public string Summary { get; set; }
public double Rating { get; set; }
public DateTime? AirDate { get; set; }
public virtual ICollection<Source> Cast { get; set; }
}
在我的方法中,首先我进行拆分然后查询。我想将查询结果添加到我的类文章的属性中,
public void AddArticle(string name, string summary = null, string sources = " ", double rating = 5, DateTime? airDate = null)
{
using (var db = this._dataContextFactory.Create())
{
var sourcesDb = sources.Split(',').Select(a => db.Sources.Where<Source>(s => s.FullName == a).Include(s => s.Articles)).ToList();
db.Articles.Add(new Article()
{
Cast = (ICollection<Source>)sourcesDb.ToList(),
AirDate = airDate,
Rating = rating,
Summary = summary,
Title = name
}); ;
db.SaveChanges();
}
}
当我执行代码时,我有以下错误:
exception {“无法施放type'system.collections.collections.generic.generic.list1 [Microsoft.entityFrameWorkCore.Query.iincludableQueryable 2 [test1project.models.models.source,System.Collections.generic.generic.generic.icollection
1 [test1project.models.models.models.models.models.models.models.tilection]]] generic.icollection
1 [test1project.models.source]'。“} system.invalidcastException
谢谢您的帮助
I am trying to add a property of a class from a query. I have two classes "Article & Source with manytomany relationship.
source:
public class Source
{
public Source()
{
this.Id = Guid.NewGuid();
}
public Guid Id { get; set; }
public int ApiId { get; set; }
public string? FullName { get; set; }
public string? Bio { get; set; }
public virtual ICollection<Article> Articles { get; set; }
}
article :
public class Article
{
public Article()
{
this.Id = Guid.NewGuid();
}
public Guid Id { get; set; }
public int ApiId { get; set; }
public string Title { get; set; }
public string Summary { get; set; }
public double Rating { get; set; }
public DateTime? AirDate { get; set; }
public virtual ICollection<Source> Cast { get; set; }
}
in my method, first I do a split then a query. I want to add the results of the query to the property Cast of my class Article
public void AddArticle(string name, string summary = null, string sources = " ", double rating = 5, DateTime? airDate = null)
{
using (var db = this._dataContextFactory.Create())
{
var sourcesDb = sources.Split(',').Select(a => db.Sources.Where<Source>(s => s.FullName == a).Include(s => s.Articles)).ToList();
db.Articles.Add(new Article()
{
Cast = (ICollection<Source>)sourcesDb.ToList(),
AirDate = airDate,
Rating = rating,
Summary = summary,
Title = name
}); ;
db.SaveChanges();
}
}
when I execute the code I have the following error :
exception {"Unable to cast object of type 'System.Collections.Generic.List1[Microsoft.EntityFrameworkCore.Query.IIncludableQueryable
2[test1Project.Models.Source,System.Collections.Generic.ICollection1[test1Project.Models.Article]]]' to type 'System.Collections.Generic.ICollection
1[test1Project.Models.Source]'."} System.InvalidCastException
Thank you for your help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
谢谢,我编辑了我的方法
var arrayofstring = sources.split(','); var sourcesdb = db.sources.where(s =&gt; arrayofstring.contains(s.fullname))。include(s =&gt; s. s. articles).tolist();
>而且有效。再次感谢 -
Thank you , I edited my method to
var arrayOfString = sources.Split(','); var sourcesDb =db.Sources.Where(s => arrayOfString.Contains(s.FullName)).Include(s => s.Articles).ToList();
and it works . Thanks again –