I have tried to make an MVC news system. I started by the use a pluralsight tutorial which was used to create a department with employees. I changed the idea to fit my own purposes, changing the departments to "category" and employees to "newspost". This all works out fine, but now I want to remove the categories, since I don't need categories for my news system. But I can't add to the database using entityframework when I do this.
我试图建立一个MVC新闻系统。我开始使用一个复数教程,用于创建一个有员工的部门。我改变了想法以适应我自己的目的,将部门改为“类别”,将员工改为“新闻报道”。这一切都很好,但现在我想删除类别,因为我不需要我的新闻系统的类别。但是当我这样做时,我无法使用entityframework添加到数据库中。
I have an interface for the datasource that looks like this:
我有一个数据源接口,如下所示:
INewMvcSiteDataSource
INewMvcSiteDataSource
public interface INewMvcSiteDataSource
{
IQueryable<NewsPost> NewsPosts { get; }
void Save();
}
This is inherited by my DB Context class:
这是我的DB Context类继承的:
NewMvcSiteDb
NewMvcSiteDb
public class NewsMvcSiteDb : DbContext, INewMvcSiteDataSource
{
public NewsMvcSiteDb() : base("DefaultConnection")
{
}
public DbSet<NewsPost> NewsPosts { get; set; }
IQueryable<NewsPost> INewMvcSiteDataSource.NewsPosts
{
get { return NewsPosts; }
}
public void Save()
{
SaveChanges();
}
}
I then want to use it in the controller to add a newspost to the database:
然后我想在控制器中使用它来向数据库添加新闻帖:
NewsController
NewsController
var newsPost = new NewsPost()
{
Subject = newsModel.Subject,
Content = newsModel.Content,
ImagePath = newsModel.ImagePath
};
_db.NewsPosts.Add(newsPost);
_db.Save();
But this is where the ADD fails with the message: 'System.Linq.IQueryable' does not contain a definition for 'Add' and no extension method 'Add' accepting a first argument of type 'System.Linq.IQueryable' could be found (are you missing a using directive or an assembly reference?)
但这是ADD失败的消息:'System.Linq.IQueryable'不包含'Add'的定义,也没有扩展方法'Add'接受第一个类型'System.Linq.IQueryable'的参数可以找到(您是否缺少using指令或程序集引用?)
Now as the error says, its caused by using IQueryable, but I have no idea how else to do it.
现在正如错误所说,它是由使用IQueryable引起的,但我不知道如何做到这一点。
Can you guys help?
你们能帮忙吗?
Thanks.
谢谢。
1 个解决方案
#1
1
If you don't mind exposing DbSet via your interface (some people don't like the ORM bleeding into the application),you should be able to do the following:
如果你不介意通过你的界面暴露DbSet(有些人不喜欢ORM流入应用程序),你应该能够做到以下几点:
public interface INewMvcSiteDataSource
{
DbSet<NewsPost> NewsPosts { get; }
void Save();
}
public class NewsMvcSiteDb : DbContext, INewMvcSiteDataSource
{
public NewsMvcSiteDb() : base("DefaultConnection")
{
}
public DbSet<NewsPost> NewsPosts { get; set; }
public void Save()
{
SaveChanges();
}
}
#1
1
If you don't mind exposing DbSet via your interface (some people don't like the ORM bleeding into the application),you should be able to do the following:
如果你不介意通过你的界面暴露DbSet(有些人不喜欢ORM流入应用程序),你应该能够做到以下几点:
public interface INewMvcSiteDataSource
{
DbSet<NewsPost> NewsPosts { get; }
void Save();
}
public class NewsMvcSiteDb : DbContext, INewMvcSiteDataSource
{
public NewsMvcSiteDb() : base("DefaultConnection")
{
}
public DbSet<NewsPost> NewsPosts { get; set; }
public void Save()
{
SaveChanges();
}
}