实体表存在,但我无法通过模型访问它

问题描述 投票:0回答:1

我在 Visual Studio 2019 中使用 C#、.NET 4.6 和 EF 6.13。添加到数据库然后刷新模型的新表开始出现问题。然后我刷新了模型。

当我用代码访问它时,我收到以下消息:

System.InvalidOperationException:实体类型 DebtCollectionBatch 不是当前上下文模型的一部分。

在 System.Data.Entity.Internal.InternalContext.UpdateEntitySetMappingsForType(类型实体类型)
在 System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(类型实体类型)
在 System.Data.Entity.Internal.Linq.InternalSet

1.Initialize()    at System.Data.Entity.Internal.Linq.InternalSet
1.get_InternalContext()
在 System.Data.Entity.Internal.Linq.InternalSet
1.ActOnSet(Action action, EntityState newState, Object entity, String methodName)   at System.Data.Entity.Internal.Linq.InternalSet
1.Add(对象实体) 在 System.Data.Entity.DbSet`1.Add(TEntity 实体)
在 D:\Repos\Suburban\ServiceApiRepo\SuburbanWebService\SuburbanWebService\SuburbanService.svc.cs 中的 SuburbanWebService.SuburbanService.SaveDebtCollectionsBatchTotal(BatchTotalDebtCollections batchTotal):第 417 行

我再次尝试刷新模型,在多次尝试使其工作失败后,我删除了该模型,保存并重新启动 VS。这次我创建了一个全新的模型。现在,我无法在代码中访问该表,至少部分地,如果这有意义的话。

我尝试添加的代码:

public bool SaveDebtCollectionsBatchTotal(BatchTotalDebtCollections batchTotal)
{
  var dt = DateTime.Now.ToUniversalTime();

  try
  {
    using (var entity = new SuburbanWebServiceEntities())
    {
      var bt = new DebtCollectionBatch();
      bt.DebtCollectionsBatchId = Guid.NewGuid();
      bt.DebtCollectionsCount = batchTotal.AchCount;
      bt.DebtCollectionsCompany = batchTotal.DebtCollectionsCompany;
      bt.CompanyCode = batchTotal.CompanyCode;
      bt.DateTimeSubmitted = DateTime.Now; // dt;
      bt.DollarTotal = batchTotal.DollarTotal;

      entity.d  <-- fails here

      entity.SaveChanges();
      WriteLog.WriteToLogFile("SaveDebtCollectionsBatchTotal",
        $"Successfully saved ach transactions for {batchTotal.CompanyCode} on {dt}. Transaction count: {batchTotal.AchCount} for ${batchTotal.DollarTotal}.");
    }

    return true;
  }
  catch (Exception ex)
  {
    WriteLog.WriteToLogFile("SaveDebtCollectionsBatchTotal",
      $"Failed to save debt collection batch total transaction for {batchTotal.CompanyCode} on {dt}. Transaction count: {batchTotal.AchCount} for ${batchTotal.DollarTotal}. See exception log.",
      ex);
    return false;
  }
}

我检查了连接字符串是否有重复项或它是否指向错误的位置,它似乎是正确的:

<connectionStrings>
    <add name="SuburbanWebServiceEntities" 
         connectionString="metadata=res://*/data.SuburbanWebServiceModel.csdl|res://*/data.SuburbanWebServiceModel.ssdl|res://*/data.SuburbanWebServiceModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=yermomshouse;initial catalog=SomeDataBase;persist security info=True;user id=youllneverguess;password=stopsnooping;MultipleActiveResultSets=True;App=EntityFramework&quot;" 
         providerName="System.Data.EntityClient" />
</connectionStrings>`

这是我的桌子:

这已经完全混乱了。我可以创建一个新的

DebtCollectionBatch
但我无法添加它,它找不到它。

有什么建议吗?

c# .net wcf entity-framework-6 sql-server-2019
1个回答
0
投票
  1. 是否有一个名为 DebtCollectionBatchContext.cs 的项目。检查代码是否存在:

public DbSet<DebtCollectionBatch> DebtCollectionBatch { get; set; };

2.可以直接在DebtCollectionsBilling.cs开头添加代码:

public DbSet<DebtCollectionBatch> DebtCollectionBatch { get; set; }

© www.soinside.com 2019 - 2024. All rights reserved.