我在Visual Studion 2013 Express for Web中使用ASP.Net MVC模板創建了一個簡單的項目。它不使用任何身份驗證。然後我安裝了EntityFramework
(v6.0.1),EntityFramework.SqlServerCompact
包。DbContext不使用ASP.Net中的SQL Server Compact進行初始化MVC
我的DbContext類很簡單:
public class EditTestContext : DbContext
{
public EditTestContext() : base("EditTestContext")
{
}
public EditTestContext(string connectionString) : base(connectionString)
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
Database.SetInitializer(
new DropCreateDatabaseIfModelChanges<EditTestContext>());
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
modelBuilder.Configurations.Add(new EditTestConfig());
}
}
實際的上下文對象在工作類的單位創建:
public class EditTestUoW:IEditTestUoW,IDisposable
{
private DbContext dbContext;
public EditTestUoW()
{
CreateDbContext();
}
private void CreateDbContext()
{
dbContext = new EditTestContext();//NEW DBCONTEXT OBJECT IS CREATED
dbContext.Configuration.LazyLoadingEnabled = false;
dbContext.Configuration.ProxyCreationEnabled = false;
dbContext.Configuration.ValidateOnSaveEnabled = false;
}
public IRepository<EditTestModel> EditTestRepo
{
get
{
return new EFRepository<EditTestModel>(dbContext);
}
}
}
正在使用的連接字符串是:
<add name="EditTestContext" connectionString="Data Source=
|DataDirectory|EditTestDb.sdf;Max Database Size=256;
Max Buffer Size=1024;File Mode=Shared Read;
Persist Security Info=False;" providerName="System.Data.SqlServerCe.4.0" />
現在,當我嘗試使用此上下文訪問任何內容時,如:
var rep=UoW.EditTestRepo;
var list=rep.GetAll().ToList();
我得到以下異常對rep.GetAll()
功能:
System.InvalidOperationException:序列不包含任何匹配的元素
在從倉庫類更深的挖掘,IQueryable
(DbSet<EditTest>
)拋出以下異常:
The context cannot be used while the model is being created. This exception may
be thrown if the context is used inside the OnModelCreating method or if the same
context instance is accessed by multiple threads concurrently. Note that instance
members of DbContext and related classes are not guaranteed to be thread safe.
我以爲這可能是由造成的10,但即使在我將其移除後它仍然存在。
我在這裏做錯了什麼(某些程序集引用等)丟失?
你救我的腦海!我爲此瘋狂了! – wilver
當我使用SQL CE調用'Property(d => d.BTCValue).HasPrecision(24,8)'方法時,我遇到了同樣的異常。感謝您澄清原因! – Andras
date,smalldate和文字似乎不太適合 –