English 中文(简体)
实体框架RC1 DbContext query
原标题:Entity Framework RC1 DbContext query issue

I m 试图利用实体框架代码第一弧1实施储存模式。 我正在讨论的问题是编造DbContext。 我有一只自动集装箱解决了国际储存地,它有一个背景提供商,仅用一个新版的DbContext和一个电线铺设。 config文档。 由于林克2sql,这一部分没有问题,但欧安会似乎正在cho。 我举一个例子说明问题。 我删除了该法典,以简化事物,因此,你看不出任何存放方式。 公正的是,在没有其他法典和课堂的情况下发生的事情。

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

PlsContext是我创建我的DbContext阶级的地方。 存放方式没有发现任何关于PlssContext的内容。 我认为,我可以做的最好办法是建立一个DbContext,说明与Sqlexpress数据库的联系,并询问这方面的数据。 在新添加了PlsContext的标语之后,从该背景中发现了联系。 因此,他们正在同一个软体压缩数据库上点。

第一组工程。 第二点错误是:

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.

页: 1

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

我的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)
            {

我用同样的错误尝试了所有的初始战略。 我并不认为数据库正在变化。 如果我删除,

     modelBuilder.Conventions.Remove<IncludeMetadataConvention>();

然后,错误回报是

实体类型用户不属于目前情况下的模式。

哪一种是有意义的。 但是,你们如何把这一切结合起来?

最佳回答

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

您可以直接使用密码

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签