The*_*iot 6 entity-framework sql-server-ce dbcontext asp.net-mvc-4
我在Visual Studion 2013 Express for Web中使用ASP.Net MVC模板创建了一个简单的项目.它不使用任何身份验证.然后我安装了EntityFramework
(v6.0.1)EntityFramework.SqlServerCompact
包.
我的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());
}
}
Run Code Online (Sandbox Code Playgroud)
实际的上下文对象是在Unit of Work类中创建的:
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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
使用的连接字符串是:
<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" />
Run Code Online (Sandbox Code Playgroud)
现在,当我尝试使用此上下文访问任何内容时:
var rep=UoW.EditTestRepo;
var list=rep.GetAll().ToList();
Run Code Online (Sandbox Code Playgroud)
我在rep.GetAll()
函数上遇到以下异常:
System.InvalidOperationException:Sequence不包含匹配元素
在深入挖掘时,IQueryable
从Repository class(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.
Run Code Online (Sandbox Code Playgroud)
我以为它可能是由ninject
它引起的,但是在我删除之后它仍然存在.
我在这里做错了什么(某些装配参考等)丢失了?
The*_*iot 11
在对该问题进行了一些其他搜索之后,我得到了这个 MSDN论坛链接.正如Rowan所建议的那样,我尝试使用我的EFRepository
类中的以下语句手动初始化上下文:
dbContext.Database.Initialize(false);
Run Code Online (Sandbox Code Playgroud)
应用程序在访问该方法之前失败了GetAll()
.但这暴露了堆栈跟踪,这给了我一些方向:
[InvalidOperationException: Sequence contains no matching element]
System.Linq.Enumerable.Single(IEnumerable`1 source, Func`2 predicate) +2614017
System.Data.Entity.Utilities.DbProviderManifestExtensions.GetStoreTypeFromName
(DbProviderManifest providerManifest, String name) +146
.....Other Lines.....
System.Data.Entity.Internal.InternalContext.Initialize() +31
System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType
(Type entityType) +38
System.Data.Entity.Internal.Linq.InternalSet`1.Initialize() +138
System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext() +38
System.Data.Entity.Infrastructure.DbQuery`1.System.Linq.IQueryable
.get_Provider() +99
System.Linq.Queryable.Any(IQueryable`1 source) +50
Run Code Online (Sandbox Code Playgroud)
然后搜索DbProviderManifestExtensions.GetStoreTypeFromName
显示这是EF试图获得列类型的行.我已UNIQUEIDENTIFIER
为我的Id列指定:
Property(x=> x.Id).HasColumnType("UNIQUEIDENTIFIER")
Run Code Online (Sandbox Code Playgroud)
一旦我对此发表评论,一切都很顺利.
虽然Codeplex上有一个请求,以便在列类型对数据库提供程序无效时提供正确的错误消息.
归档时间: |
|
查看次数: |
6696 次 |
最近记录: |