带有现有数据库表的代码优先通用存储库 [英] Code First Generic Repository with existing Database tables

查看:44
本文介绍了带有现有数据库表的代码优先通用存储库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个通用存储库类,该类首先使用代码来执行数据操作。

I have a Generic Repository class using code first to perform data operations.

public class GenericRepository<T> where T : class
{

public DbContext _context = new DbContext("name=con");

private DbSet<T> _dbset;

public DbSet<T> Dbset
{

    set { _dbset = value; }
    get
    {
        _dbset = _context.Set<T>();
        return _dbset;
    }

}

public IQueryable<T> GetAll()
{
    return Dbset;
}

} 

我有一个实体班级的老师,它将映射到数据库中具有相同字段的现有表 Teacher。

I have an entity class Teacher, which maps to an existing table "Teacher" in my database, with exactly the same fields.

public class Teacher
{
public Teacher()
{
    //
    // TODO: Add constructor logic here
    //
}

public int TeacherID { get; set; }

public string FirstName { get; set; }

public string LastName { get; set; }

public int Age { get; set; }
}

我下面的代码将来自Teacher的数据绑定到转发器控件

I have the following code below which binds data from Teacher to a repeater control.

 GenericRepository<Teacher> studentrepository = new GenericRepository<Teacher>();
    rptSchoolData.DataSource = studentrepository.GetAll().ToList();
    rptSchoolData.DataBind();

但是我得到了一个异常异常在当前上下文中,实体类型Teacher不是该模型的一部分。首先使用现有数据库进行代码时,我还需要做其他工作吗?

But I get an exception exception "The entity type Teacher is not part of the model in the current context". Do I have to do any additional work when using an existing database for code first?

推荐答案

您必须创建一个从 DbContext 派生的上下文类。该类应具有类型 DbSet< T> 的属性,该属性将为EF提供足够的信息,以使用默认命名和关联约定创建数据库并与之通信。它将使用 Student.Teacher 之类的属性(如果有)来推断外键关联:

You must create a context class that derives from DbContext. The class should have properties of type DbSet<T> which will give EF enough information to create and communicate with a database with default naming and association conventions. It will use properties like Student.Teacher (if any) to infer foreign key associations:

public class MyContext: DbContext
{
    public DbSet<Teacher> Teachers { get; set; }
    public DbSet<Student> Students { get; set; }
    ...
}

如果默认值不是您想要的,或者当您有一个想要与模型中的名称和关联匹配的现有数据库时,您可以做两件(或三件)的事情:

If the defaults are not what you want, or when you've got an existing database that you want to match with the names and associations in your model you can do two (or three) things:


  • 覆盖 OnModelCreating 手动配置映射。就像数据库中的表具有丑陋的前缀(提醒人们在看到表时看到表一样):

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Teacher>()
                    .Map(e => e.ToTable("tblTeacher"));
        ...
    }




  • (较少(有利)使用数据批注进行相同的操作。

  • 将其转过来并使用实体框架Powertools ,即可将数据库反向工程为包含流利映射和DbContext派生上下文的类模型。修改现有模型可能比从头开始更容易。

    • (Less favorable) Use data annotations to do the same.
    • Turn it around and use Entity Framework Powertools to reverse-engineer a database into a class model including fluent mappings and a DbContext-derived context. Maybe easier to modify an existing model than to start from scratch.
    • 这篇关于带有现有数据库表的代码优先通用存储库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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