C# 添加实体时实体框架错误与表名

C# 添加实体时实体框架错误与表名,c#,entity-framework-6,C#,Entity Framework 6,我有表BookComments和FileComments。我有实体类BookComment和FileComment 当我得到数据时,一切都很好。但当我尝试添加数据时,我得到了错误。找不到BooksComments表或filecomments。我尝试使用TableAttribute类,但没有帮助我 我使用实体框架6.1.3 更新 如果我将表名从BookComments重命名为bookcomment。我无法获取数据并获取错误信息 对象名称dbo.BookComments无效 添加实体 pub

我有表BookCommentsFileComments。我有实体类BookCommentFileComment

当我得到数据时,一切都很好。但当我尝试添加数据时,我得到了错误。找不到BooksComments表或filecomments。我尝试使用TableAttribute类,但没有帮助我

我使用实体框架6.1.3

更新

如果我将表名从BookComments重命名为bookcomment。我无法获取数据并获取错误信息

对象名称dbo.BookComments无效

添加实体

    public void AddEntity(T entity)
    {
        this.Entities.Add(entity);
    }

    protected IDbSet<T> Entities
    {
            get
            {
                return this._entities ?? (this._entities = this._context.Set<T>());
            }
public void AddEntity(T实体)
{
本.实体.添加(实体);
}
受保护的IDbSet实体
{
得到
{
返回此._entities???(此._entities=此._context.Set());
}
添加注释簿控制器类

 public async Task<JsonResult> AddComment(int id, Comment comment)
        {
            try
            {
                CommentItem addComment = await Task.Run(() =>
                        {
                            var newcomment = this._booksService.AddComment(id, comment);
                            return AutoMapManager.GetMap<CommentItem, IComment>(newcomment);
                        });

                return this.Json(new { Result = true, comment = addComment }, JsonRequestBehavior.AllowGet);
            }
            catch (Exception e)
            {
                return this.Json(new { Result = false, e.GetBaseException().Message }, JsonRequestBehavior.AllowGet);
            }
        }
        }
 public IComment AddComment<T>(int bookId, T item) where T : new()
    {
        var comment = AutoMapManager.GetMap<BookComment, T>(item);
        comment.BookId = bookId;

        this._commentRepository.AddEntity(comment);
        this._commentRepository.SaveChanges();
        return comment;
    }
公共异步任务AddComment(int-id,Comment-Comment)
{
尝试
{
CommentItem addComment=等待任务。运行(()=>
{
var newcomment=this.\u booksService.AddComment(id,comment);
返回AutoMapManager.GetMap(newcomment);
});
返回this.Json(新的{Result=true,comment=addComment},JsonRequestBehavior.AllowGet);
}
捕获(例外e)
{
返回this.Json(新的{Result=false,e.GetBaseException().Message},JsonRequestBehavior.AllowGet);
}
}
}
图书服务类

 public async Task<JsonResult> AddComment(int id, Comment comment)
        {
            try
            {
                CommentItem addComment = await Task.Run(() =>
                        {
                            var newcomment = this._booksService.AddComment(id, comment);
                            return AutoMapManager.GetMap<CommentItem, IComment>(newcomment);
                        });

                return this.Json(new { Result = true, comment = addComment }, JsonRequestBehavior.AllowGet);
            }
            catch (Exception e)
            {
                return this.Json(new { Result = false, e.GetBaseException().Message }, JsonRequestBehavior.AllowGet);
            }
        }
        }
 public IComment AddComment<T>(int bookId, T item) where T : new()
    {
        var comment = AutoMapManager.GetMap<BookComment, T>(item);
        comment.BookId = bookId;

        this._commentRepository.AddEntity(comment);
        this._commentRepository.SaveChanges();
        return comment;
    }
public IComment AddComment(int bookId,T item),其中T:new()
{
var comment=AutoMapManager.GetMap(项目);
comment.BookId=BookId;
此._commentRepository.AddEntity(注释);
此._commentRepository.SaveChanges();
回复评论;
}

检查以确保SQL中的表名与模型具有相同的大小写敏感度。实体框架使用SQL(通常不区分大小写)进行查询。。。这就是为什么您可以检索数据。但在添加行时,EF使用CLR(区分大小写)查找表实体。首先看看这是否是您的问题。

Arw有两个打字错误,或者错误是否真的是BooksComments和FileComments?如果是这样,问题就很明显了。正如您所说,您的表是BookComments(不是BookComments)和FileComments(不是FileComments)。请参阅我在EF中命名表的问题:我如何看到我可以获得数据,只是添加新数据的问题。如果我将表名重命名为BooksComments。我可以读取数据并获取错误无效的对象名dbo.bookComments。问题可能出在您的对象模型上。发布类中的代码以及insert和get方法。我使用泛型存储库,还有ArticleComments,我没有问题。我发现了新的问题。我有论坛,名称空间是SGN.Modules.Forums.Entities。但当我尝试获取数据时,我得到了错误。因为EF尝试在SGN.CorePost中获取论坛实体。很难猜出问题出在哪里而不去发现它。