实体框架RC1的DbContext查询问题 [英] Entity Framework RC1 DbContext query issue

查看:113
本文介绍了实体框架RC1的DbContext查询问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现使用实体框架代码RC首先1.我遇到与创造的DbContext问题库模式。我有一个IoC容器解决IRepository,它有一个contextprovider刚刚新闻与在windsor.config文件的连接字符串的新的DbContext。随着LINQ2SQL这部分没有问题,但EF似乎窒息。我会用下面的例子说明问题。我掏出代码把事情简单化了一点,所以这就是为什么你在这里看不到任何存储库模式的东西。只是八九不离十正在发生的事情没有所有的额外的代码和类。

I'm trying to implement the repository pattern using entity framework code first rc 1. The problem I am running into is with creating the DbContext. I have an ioc container resolving the IRepository and it has a contextprovider which just news up a new DbContext with a connection string in a windsor.config file. With linq2sql this part was no problem but EF seems to be choking. I'll describe the problem below with an example. I've pulled out the code to simplify things a bit so that is why you don't see any repository pattern stuff here. just sorta what is happening without all the extra code and classes.

            using (var context = new PlssContext())
            {
                var x = context.Set<User>();
                var y = x.Where(u => u.UserName == LogOnModel.UserName).FirstOrDefault();
            }

            using (var context2 = new DbContext(@"Data Source=.\SQLEXPRESS;Initial Catalog=PLSS.Models.PlssContext;Integrated Security=True;MultipleActiveResultSets=True"))
            {
                var x = context2.Set<User>();
                var y = x.Where(u => u.UserName == LogOnModel.UserName).FirstOrDefault();
            }



PlssContext就是我创造我的DbContext类。 Repository模式不知道什么PlssContext。我想我能做的最好是建立一个的DbContext与连接字符串SQLEXPRESS数据库和查询数据的方式。在var context2的连接字符串newing了PlssContext对象后,从上下文抓起。因此,他们都在同一数据库SQLEXPRESS指向。

PlssContext is where I am creating my DbContext class. The repository pattern doesn't know anything about PlssContext. The best I thought I could do was create a DbContext with the connection string to the sqlexpress database and query the data that way. The connection string in the var context2 was grabbed from the context after newing up the PlssContext object. So they are pointing at the same sqlexpress database.

第一个查询工作。第二个查询,此错误惨遭失败:

The first query works. The second query fails miserably with this error:

该模型来头的DbContext'
上下文,因为数据库$ B改变$ b已创建。手动
删除/更新数据库,或用
IDatabaseInitializer实例调用
Database.SetInitializer。对于
例如,
DropCreateDatabaseIfModelChanges
策略会自动用新的数据删除和
重新创建数据库,以及可选
种子吧。

The model backing the 'DbContext' context has changed since the database was created. Either manually delete/update the database, or call Database.SetInitializer with an IDatabaseInitializer instance. For example, the DropCreateDatabaseIfModelChanges strategy will automatically delete and recreate the database, and optionally seed it with new data.

在此行

var y = x.Where(u => u.UserName == LogOnModel.UserName).FirstOrDefault();

下面是我的DbContext

Here is my DbContext

namespace PLSS.Models
{
    public class PlssContext : DbContext
    {
        public DbSet<User> Users { get; set; }
        public DbSet<Corner> Corners { get; set; }
        public DbSet<Lookup_County> Lookup_County { get; set; }
        public DbSet<Lookup_Accuracy> Lookup_Accuracy { get; set; }
        public DbSet<Lookup_MonumentStatus> Lookup_MonumentStatus { get; set; }
        public DbSet<Lookup_CoordinateSystem> Lookup_CoordinateSystem { get; set; }

        public class Initializer : DropCreateDatabaseAlways<PlssContext>
        {
            protected override void Seed(PlssContext context)
            {

我已经尝试了所有的同样的错误初始化策略。我不认为该数据库正在发生变化。如果我删除

I've tried all of the Initializer strategies with the same errors. I don't think the database is changing. If I remove the

     modelBuilder.Conventions.Remove<IncludeMetadataConvention>();



然后错误再次是

Then the error returns is

的实体类型的用户是不是该机型为当前上下文的一部分。

The entity type User is not part of the model for the current context.

哪种类型的有道理。但你如何把这个一起?

Which sort of makes sense. But how do you bring this all together?

推荐答案

这是正确的行为。平原的DbContext 没有关于映射的想法(=不知道您的任何实体)。这就是为什么你总是应该在创建派生背景的原因。你的仓库不知道 PlssContext ,但你仍然可以注入它喜欢的:

That is correct behavior. Plain DbContext has no idea about mappings (= doesn't know any of your entities). That is the reason why you should always create derived context. Your repository doesn't know about PlssContext but you can still inject it like:

public class Repository
{
  private readonly DbContext _context;

  public Repository(DbContext context)
  {
    _context = context;
  }
  ...
}

var repository = new Repository(new PlssContext());

您不能使用基地的DbContext 实例当直接先用代码。

You can't use base DbContext instance directly when using code first.

这篇关于实体框架RC1的DbContext查询问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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