开发者

Entity Framework RC1 DbContext query issue

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 se开发者_开发技巧ems 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 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:

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.

on this line

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

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?


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());

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜