当我尝试选择列表中的所有项目时,我得到一个空列表。
这是我的
Business.Classification
班级,我称之为ToList()
public class Classification
{
private DatabaseContext db;
public Classification()
{
if (db == null)
db = new DatabaseContext();
}
public int getClassification()
{
using (db)
{
List<Classification> a = db.ClassificationContext.ToList();
return a.Count;
}
}
}
这是我的模型课程:
[Table("Classification")]
public class Classification
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.None)]
[Column]
public Guid ClassificationId { get; set; }
[Column]
public string ClassificationName { get; set; }
}
这是我的 Context 类:
public class DatabaseContext : DbContext
{
public DbSet<Classification> ClassificationContext { get; set; }
}
这是我的业务项目的应用程序配置文件:
<configuration>
<connectionStrings>
<add name="Conection" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Guilherme\Documents\Database.mdf;Integrated Security=True;Connect Timeout=30" providerName="System.Data.EntityClient"/>
</connectionStrings>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
</configuration>
您的连接字符串可能会导致此问题。
<connectionStrings>
<add name="DatabaseContext" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Guilherme\Documents\Database.mdf;Integrated Security=True;Connect Timeout=30" providerName="System.Data.EntityClient"/>
</connectionStrings>
试试这个,可能是你使用上下文的方式有问题
`
using (DatabaseContext context = new DatabaseContext())
{
return context.tablename.tolist().count;
}
`
我花了将近半天的时间来解决类似的问题。结果我没有在oracle数据库上运行提交。这个链接帮助了我。