C# 实体框架错误。基础提供程序在打开时失败,无法附加文件
我有一个简单的asp.net mvc项目,它应该在app data文件夹中创建一个数据库,但是我得到了下面的错误 {“基础提供程序在打开时失败。”}{“无法附加该文件。” 'F:\GoogleDriveSync\products\U WB0R5L90S\MVC5\U Full\U VersionCapatech\Inspinia\U MVC5\App\U Data\TokenCacheDataContext.mdf' 作为数据库“TokenCacheDataContext” 代码基于这个github项目 我的代码如下: EfAdalTokenCacheC# 实体框架错误。基础提供程序在打开时失败,无法附加文件,c#,asp.net,asp.net-mvc,entity-framework,asp.net-mvc-5,C#,Asp.net,Asp.net Mvc,Entity Framework,Asp.net Mvc 5,我有一个简单的asp.net mvc项目,它应该在app data文件夹中创建一个数据库,但是我得到了下面的错误 {“基础提供程序在打开时失败。”}{“无法附加该文件。” 'F:\GoogleDriveSync\products\U WB0R5L90S\MVC5\U Full\U VersionCapatech\Inspinia\U MVC5\App\U Data\TokenCacheDataContext.mdf' 作为数据库“TokenCacheDataContext” 代码基于这个gith
public class EfAdalTokenCache : TokenCache
{
private TokenCacheDataContext db = new TokenCacheDataContext();
string User;
private PerUserWebCache Cache;
// constructor
public EfAdalTokenCache(string user)
{
// associate the cache to the current user of the web app
User = user;
this.AfterAccess = AfterAccessNotification;
this.BeforeAccess = BeforeAccessNotification;
this.BeforeWrite = BeforeWriteNotification;
// look up the entry in the DB
Cache = db.PerUserCacheList.FirstOrDefault(c => c.WebUserUniqueId == User);
// place the entry in memory
this.Deserialize((Cache == null) ? null : Cache.CacheBits);
}
// clean up the DB
public override void Clear()
{
base.Clear();
foreach (var cacheEntry in db.PerUserCacheList)
db.PerUserCacheList.Remove(cacheEntry);
db.SaveChanges();
}
// Notification raised before ADAL accesses the cache.
// This is your chance to update the in-memory copy from the DB, if the in-memory version is stale
void BeforeAccessNotification(TokenCacheNotificationArgs args)
{
if (Cache == null)
{
// first time access
Cache = db.PerUserCacheList.FirstOrDefault(c => c.WebUserUniqueId == User);
}
else
{ // retrieve last write from the DB
var status = from e in db.PerUserCacheList
where (e.WebUserUniqueId == User)
select new
{
LastWrite = e.LastWrite
};
// if the in-memory copy is older than the persistent copy
if (status.First().LastWrite > Cache.LastWrite)
//// read from from storage, update in-memory copy
{
Cache = db.PerUserCacheList.FirstOrDefault(c => c.WebUserUniqueId == User);
}
}
this.Deserialize((Cache == null) ? null : Cache.CacheBits);
}
// Notification raised after ADAL accessed the cache.
// If the HasStateChanged flag is set, ADAL changed the content of the cache
void AfterAccessNotification(TokenCacheNotificationArgs args)
{
// if state changed
if (this.HasStateChanged)
{
Cache = new PerUserWebCache
{
WebUserUniqueId = User,
CacheBits = this.Serialize(),
LastWrite = DateTime.Now
};
//// update the DB and the lastwrite
db.Entry(Cache).State = Cache.EntryId == 0 ? EntityState.Added : EntityState.Modified;
db.SaveChanges();
this.HasStateChanged = false;
}
}
void BeforeWriteNotification(TokenCacheNotificationArgs args)
{
// if you want to ensure that no concurrent write take place, use this notification to place a lock on the entry
}
}
错误将在上面的
Cache = db.PerUserCacheList.FirstOrDefault(c => c.WebUserUniqueId == User);
Global.asax
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
Database.SetInitializer(new TokenCacheInitializer());
}
令牌初始值设定项
public class TokenCacheInitializer : System.Data.Entity.DropCreateDatabaseIfModelChanges<TokenCacheDataContext>
{
}
请检查这个
还要检查您的连接字符串
打开“开始/程序”菜单下的“Visual Studio的开发人员命令提示符”,需要管理员权限。运行以下命令:
sqllocaldb.exe stop v11.0
sqllocaldb.exe delete v11.0
之后,启动MVC应用程序可能重复的
public class PerUserWebCache
{
[Key]
public int EntryId { get; set; }
public string WebUserUniqueId { get; set; }
public byte[] CacheBits { get; set; }
public DateTime LastWrite { get; set; }
}
sqllocaldb.exe stop v11.0
sqllocaldb.exe delete v11.0