Tôi đã tạo một dự án đơn giản bằng cách sử dụng mẫu ASP.Net MVC trong Visual Studion 2013 Express cho Web. Nó không sử dụng bất kỳ xác thực nào. Sau đó, tôi đã cài đặt các gói EntityFramework
(v6.0.1), EntityFramework.SqlServerCompact
.DbContext không khởi tạo với SQL Server Compact trong ASP.Net MVC
My DbContext lớp là rất đơn giản:
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());
}
}
Đối tượng bối cảnh thực tế được tạo ra trong các đơn vị của lớp làm việc:
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);
}
}
}
Chuỗi kết nối được sử dụng là:
<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" />
Bây giờ, khi tôi cố gắng truy cập bất kỳ nội dung nào bằng ngữ cảnh này như:
var rep=UoW.EditTestRepo;
var list=rep.GetAll().ToList();
Tôi nhận được ngoại lệ sau đây trên rep.GetAll()
chức năng:
System.InvalidOperationException: Chuỗi không chứa yếu tố phù hợp
On đào sâu hơn, IQueryable
từ lớp Repository (DbSet<EditTest>
) được ném ngoại lệ sau đây:
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.
Tôi nghĩ rằng nó có thể đã gây ra bởi ninject
, nhưng nó vẫn còn đó ngay cả sau khi tôi gỡ bỏ nó.
Những gì tôi đang làm sai ở đây hoặc một cái gì đó (một số tham chiếu lắp ráp, vv) bị thiếu?
bạn hãy lưu tâm trí của mình! Tôi đã phát điên vì điều đó !! – wilver
Tôi có cùng ngoại lệ khi tôi gọi phương thức 'Property (d => d.BTCValue) .HasPrecision (24, 8)' với SQL CE. Cảm ơn bạn đã đưa ra lý do rõ ràng! – Andras
ngày, smalldate và văn bản dường như không phù hợp quá tốt cũng như –