Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 意外CodeFirstException |首先使用数据库使用efforce.Ef6进行实体框架单元测试 形势_C#_Entity Framework_Unit Testing_Entity Framework 6_Effort - Fatal编程技术网

C# 意外CodeFirstException |首先使用数据库使用efforce.Ef6进行实体框架单元测试 形势

C# 意外CodeFirstException |首先使用数据库使用efforce.Ef6进行实体框架单元测试 形势,c#,entity-framework,unit-testing,entity-framework-6,effort,C#,Entity Framework,Unit Testing,Entity Framework 6,Effort,我想基于entity framework 6启用我的系统的单元测试。我已经用数据库优先的方法创建了我的DbContext和我的模型,现在有了一个.edmx设计器文件 问题 我自动创建的DbContext会覆盖如下方法: protected override void OnModelCreating(DbModelBuilder modelBuilder) { throw new UnintentionalCodeFirstException(); } public DatabaseEn

我想基于entity framework 6启用我的系统的单元测试。我已经用数据库优先的方法创建了我的
DbContext
和我的模型,现在有了一个.edmx设计器文件

问题 我自动创建的
DbContext
会覆盖如下方法:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    throw new UnintentionalCodeFirstException();
}
public DatabaseEntities(DbConnection connection) 
     : base(connection, true) { }
我正在尝试基于的信息在模拟数据访问服务中创建我的
DbContext
的新实例。我的代码没有运行到
Database.SetInitializerDbContext
初始化如下所示:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    throw new UnintentionalCodeFirstException();
}
public DatabaseEntities(DbConnection connection) 
     : base(connection, true) { }
当到达前面提到的在ModelCreating上覆盖的
时,会导致以下异常

System.Data.Entity.Infrastructure.CodeFirstException:“上下文正在代码优先模式下使用,代码是从EDMX文件生成的,用于数据库优先或模型优先开发。这将无法正常工作。要解决此问题,请不要删除引发此异常的代码行。如果希望首先使用数据库或模型,请确保启动项目的app.config或web.config中包含实体框架连接字符串。如果要创建自己的DbConnection,请确保它是EntityConnection而不是其他类型的DbConnection,并将其传递给采用DbConnection的一个基本DbContext构造函数。要了解有关代码优先、数据库优先和模型优先的更多信息,请参阅此处的实体框架文档:'

关于stackoverflow,有很多问题都集中在使用database first apporach时进行的单元测试上,但似乎没有人像我这样有类似的问题

我已经试过做的事
  • 在互联网上搜索很长一段时间寻找解决方案
  • 在modelCreating()
上取消对重写的
的注释
  • 我已经试过申请,但没有成功
  • 使用Mock my
    DbContext
    ,这绝对不是一个选项
  • 问题 如何创建和使用内存中的数据库(费劲)


    其他信息

    当我取消注释OnModelCreating()上的
    时,在第一次访问DataContext时将引发异常。例如:

    DbConnection effortConnection = Effort.DbConnectionFactory.CreatePersistent("MockedDatabaseEntities");
    
    using (DatabaseEntities dataContext = new DatabaseEntities(effortConnection))
    {
        dataContext.Students.Count(); // Exception throws here
    }
    
    System.Data.Entity.ModelConfiguration.ModelValidationException:“在模型生成过程中检测到一个或多个验证错误: MyApplication.Shared.Model.KeyValuePair::EntityType“KeyValuePair”未定义密钥。定义此EntityType的键。 KeyValuePairs:EntityType:EntitySet“KeyValuePairs”基于未定义键的类型“KeyValuePair”


    问题是我必须为KeyValuePair数据集指定密钥

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<KeyValuePair>().HasKey(x => x.Key);
        base.OnModelCreating(modelBuilder, null);
    }
    
    模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
    {
    modelBuilder.Entity().HasKey(x=>x.Key);
    OnModelCreating(modelBuilder,null);
    }
    

    多谢各位

    如果有人在寻找db-first方法的解决方案,这对我很有用:

    string connStr=@“metadata=res://*/Model.csdl | res://*/Model.ssdl | res://*/Model.msl;”;
    DbConnection=EntityConnectionFactory.CreateTransient(connStr);
    返回新的MyDatabaseContext(连接);
    
    我从我的web/app.config获得了
    connStr
    部分。

    另外,我没有触及OnModelCreating的

    当OnModelCreating没有注释时会出现什么问题(因为我们需要以某种方式消除它)?如果它只是在每次构建模型时重新创建的,那么您就可以对数据库实体进行子类化,并使用不会爆炸的内容再次覆盖它;如果您在内存数据库(瞬态)中使用pr测试,则在测试期间ownsDbConnection应该为false(基本构造函数的布尔参数)。如果其他所有操作都失败,我将在db-first'ish环境中工作,但我手动将实体映射到SQL表。@RobertJørgensgaardEngdahl-我已更新了我的问题。提前谢谢你!从错误“EntityType'KeyValuePair'未定义密钥”中,您必须以某种方式指示EntityFramework您有一个密钥。你可以在OnModelCreating中这样做,就像这里一样