Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.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# 初始化没有键的数据库集时出错:无法跟踪类型为';MyEntity';因为它没有主键_C#_Entity Framework_Unit Testing_Xunit - Fatal编程技术网

C# 初始化没有键的数据库集时出错:无法跟踪类型为';MyEntity';因为它没有主键

C# 初始化没有键的数据库集时出错:无法跟踪类型为';MyEntity';因为它没有主键,c#,entity-framework,unit-testing,xunit,C#,Entity Framework,Unit Testing,Xunit,我有一个DbContext,它有一个Dbset,没有键。它在只查询表的应用程序中工作 public class MyDbContext : DbContext, IMyDbContext { public MyDbContext(DbContextOptions<MyDbContext> options) : base(options) { } public DbSet<MyEntity> MyEntities { get; set; } prot

我有一个
DbContext
,它有一个
Dbset
,没有键。它在只查询表的应用程序中工作

public class MyDbContext : DbContext, IMyDbContext
{
    public MyDbContext(DbContextOptions<MyDbContext> options) : base(options) { }
    public DbSet<MyEntity> MyEntities { get; set; }
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<MyEntity>().HasNoKey(); // No key
        modelBuilder.ApplyConfigurationsFromAssembly(typeof(MyDbContext).Assembly);
    }
}
但是,在实际运行测试之前,运行任何测试方法都会出现以下错误。错误发生在上面的context.MyEnities.AddRange(..行上

Message: System.AggregateException : One or more errors occurred. (Unable to track an instance of type 'MyEntity' because it does not have a primary key. Only entity types with primary keys may be tracked.) (The following constructor parameters did not have matching fixture data: QueryTestFixture fixture) ---- System.InvalidOperationException : Unable to track an instance of type 'MyEntity' because it does not have a primary key. Only entity types with primary keys may be tracked. ---- The following constructor parameters did not have matching fixture data: QueryTestFixture fixture Stack Trace: ----- Inner Stack Trace #1 (System.InvalidOperationException) ----- StateManager.GetOrCreateEntry(Object entity) DbContext.SetEntityStates(IEnumerable`1 entities, EntityState entityState) DbContext.AddRange(IEnumerable`1 entities) DbContext.AddRange(Object[] entities) InternalDbSet`1.AddRange(TEntity[] entities) MyDbContextFactory.Create() line 20 QueryTestFixture.ctor() line 16 ----- Inner Stack Trace #2 (Xunit.Sdk.TestClassException) ----- 信息: System.AggregateException:发生一个或多个错误。(无法跟踪“MyEntity”类型的实例,因为它没有主键。只能跟踪具有主键的实体类型。)(以下构造函数参数没有匹配的装置数据:QueryTestFixture fixture) ----System.InvalidOperationException:无法跟踪“MyEntity”类型的实例,因为它没有主键。只能跟踪具有主键的实体类型。 ----以下构造函数参数没有匹配的装置数据:QueryTestFixture fixture 堆栈跟踪: -----内部堆栈跟踪#1(系统无效操作异常)----- StateManager.GetOrCreateEntry(对象实体) SetEntityState(IEnumerable`1实体,EntityState EntityState) DbContext.AddRange(IEnumerable`1实体) DbContext.AddRange(对象[]实体) InternalDbSet`1.AddRange(tenty[]实体) MyDbContextFactory.Create()第20行 QueryTestFixture.ctor()第16行 -----内部堆栈跟踪#2(Xunit.Sdk.TestClassException)-----
到目前为止,这是实体框架的一个开放问题:

不能在DbQuery或没有键的DbSet上添加新实体。
我建议您继续关注这个问题,现在模拟一下您的上下文,或者使用它来完成这个任务。

下面的解决方案对我来说很有效

对于场景#1和#2,您可以在ModelCreating方法上向DbContext模块添加以下行:modelBuilder.Entity().HasKey(x=>new{x.column_a,x.column_b})//使记录唯一所需的列数

查询的详细信息在tis url上


.

你想测试什么?为什么不使用IMyDbContext的模拟,而使用MyDbContext的具体实例?你试过使用
AsNoTracking
进行查询吗?@ca9163d9在实际应用程序DbSet上映射视图或表?@LuttiCoelho,这是一个视图。@PavelAnikhouski,在哪里添加
AsNoTracking
?我试过将它(`context.MyEnities.AsNoTracking();
)添加到context.MyEnities.AddRange(…)之前,但它仍然得到了错误。
[Collection("QueryTests")]
public class MyTest
{
    private readonly MyDbContext _context;
    private readonly IMapper _mapper;

    public MyTest(QueryTestFixture fixture)
    {
        _context = fixture.MyDbContext;
        _mapper = fixture.Mapper;
    }
}
Message: System.AggregateException : One or more errors occurred. (Unable to track an instance of type 'MyEntity' because it does not have a primary key. Only entity types with primary keys may be tracked.) (The following constructor parameters did not have matching fixture data: QueryTestFixture fixture) ---- System.InvalidOperationException : Unable to track an instance of type 'MyEntity' because it does not have a primary key. Only entity types with primary keys may be tracked. ---- The following constructor parameters did not have matching fixture data: QueryTestFixture fixture Stack Trace: ----- Inner Stack Trace #1 (System.InvalidOperationException) ----- StateManager.GetOrCreateEntry(Object entity) DbContext.SetEntityStates(IEnumerable`1 entities, EntityState entityState) DbContext.AddRange(IEnumerable`1 entities) DbContext.AddRange(Object[] entities) InternalDbSet`1.AddRange(TEntity[] entities) MyDbContextFactory.Create() line 20 QueryTestFixture.ctor() line 16 ----- Inner Stack Trace #2 (Xunit.Sdk.TestClassException) -----