实体框架将s添加到了我的.dbo [英] Entity Framework added s to my .dbo

查看:58
本文介绍了实体框架将s添加到了我的.dbo的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在发现wars.dbo异常时使用了"Entity Framework DbContext".这很奇怪,因为我一直在我的网站上询问towar.dbo,但没有询问towars.dbo.您知道哪里出问题了吗?

I using "Entity Framework DbContext" at the moment I have got exception towars.dbo was not found. This is very strange because in my website I all the time ask about towar.dbo but no towars.dbo Do you know where is a problem?

- InnerException    {"Invalid object name 'dbo.Towars'."}   System.Exception {System.Data.SqlClient.SqlException}

关于Towar的所有事情(在程序中当然有不同的位置):

My all things about Towar (of course different place in my program):

public class ProductController : Controller
{
    //
    // GET: /Product/
        public ITowarRepository repository;

        public ProductController(ITowarRepository productRepository) 
        {
        repository = productRepository;
        }

        public ViewResult List()
        {
            return View(repository.Towar);
        }

}


public interface ITowarRepository
    {
        IQueryable<Towar> Towar { get; }
    }

public DbSet<Towar> Towar { get; set; }

public class EFTowarRepository : ITowarRepository
    {
        public EFDbContext context = new EFDbContext();
        public IQueryable<Towar> Towar
        {
            get { return context.Towar; }
        }
    }
public class Towar
    {
        [Key]
        public int Id_tow { get; set; }
        public string Nazwa { get; set; }
        public string Opis { get; set; }
        public decimal Cena { get; set; }
        public int Id_kat { get; set; }
    }

推荐答案

您可以使用流利的API覆盖DBContext类中的OnModelCreating方法,从而告诉EF映射到表Towar:

You can tell EF to map to the table Towar by overriding the OnModelCreating method in your DBContext class with fluent API like this:

public class EFDbContext : DbContext
{
   protected override void OnModelCreating(DbModelBuilder modelBuilder)
   {
      modelBuilder.Entity<Towar>().ToTable("Towar");
   }
}

现在,EF将查找Towar表而不是Towars.如果您没有创建这些表,那么您将遇到其他问题.

Now EF will look for Towar table instead of Towars. If you do not have these tables created, there is some other problem you are having.

这篇关于实体框架将s添加到了我的.dbo的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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