gpt4 book ai didi

c# - Entity Framework 错误。基础提供程序打开失败无法附加文件

转载 作者:太空宇宙 更新时间:2023-11-03 10:30:03 35 4
gpt4 key购买 nike

我有一个简单的 asp.net mvc 项目,它应该在应用程序数据文件夹上创建一个数据库,但我得到以下错误

{"The underlying provider failed on Open."} {"Cannot attach the file 'F:\GoogleDriveSync\products_WB0R5L90S\MVC5_Full_VersionCapatech\Inspinia_MVC5\App_Data\TokenCacheDataContext.mdf' as database 'TokenCacheDataContext'.

代码基于这个github项目 https://github.com/andrewconnell/azadaspnetmvcauth

而我的代码如下:EfAdalTokenCache

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);

全局.asax

 protected void Application_Start()
{

AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);

Database.SetInitializer(new TokenCacheInitializer());
}

token 缓存初始化器

 public class TokenCacheInitializer : System.Data.Entity.DropCreateDatabaseIfModelChanges<TokenCacheDataContext>
{
}

token 缓存数据上下文

public class TokenCacheDataContext : DbContext
{
public TokenCacheDataContext()
: base("TokenCacheDataContext")
{ }

public DbSet<PerUserWebCache> PerUserCacheList { get; set; }

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
}

模型 PerUserWebCache

public class PerUserWebCache
{
[Key]
public int EntryId { get; set; }
public string WebUserUniqueId { get; set; }
public byte[] CacheBits { get; set; }
public DateTime LastWrite { get; set; }
}

最佳答案

请检查这个link

同时检查您的连接字符串。

在您需要管理员权限的开始/程序菜单下打开“Visual Studio 开发人员命令提示符”。运行以下命令:

sqllocaldb.exe stop v11.0

sqllocaldb.exe delete v11.0

之后,启动您的 MVC 应用程序

关于c# - Entity Framework 错误。基础提供程序打开失败无法附加文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30508561/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com