使用实体框架创建数据库失败:系统找不到指定的文件
本文关键字:找不到 系统 文件 失败 实体 框架 创建 数据库 | 更新日期: 2023-09-27 17:51:03
我得到这个错误:
An exception of type 'System.Data.SqlClient.SqlException' occurred in EntityFramework.dll but was not handled in user code
Additional information: Directory lookup for the file "C:'pub'LSK'Dev'src'LSK.Services'LSK.Services'App_Data'LSK.Packets.mdf" failed with the operating system error 2(The system can not find the specified file).
CREATE DATABASE failed. Some file names listed could not be created. Check related errors.
using (var context = new LDTContext())
{
context.Packets.AddRange(packets); // Here occurs the exception
context.SaveChanges();
}
public class LDTContext : DbContext
{
public LDTContext()
: base("name=LDTContext")
{
Configuration.LazyLoadingEnabled = true;
Configuration.ProxyCreationEnabled = true;
Configuration.ValidateOnSaveEnabled = true;
Configuration.AutoDetectChangesEnabled = true;
Configuration.UseDatabaseNullSemantics = false;
}
public DbSet<Packets> Packets{ get; set; }
}
在我的app.config:
<add name="LDTContext" connectionString="Server=(LocalDb)'LSK;Initial Catalog=LSK.Packets;Integrated Security=true;AttachDBFilename=|DataDirectory|'LSK.Packets.mdf" providerName="System.Data.SqlClient" />
为什么当数据被推入表时,数据库不是在第一个AddRange()上创建的?
您的连接字符串错误[数据源=(LocalDb)'v11.0而不是服务器=(LocalDb)'LSK]。试着
connectionString="Data Source=(LocalDb)'v11.0;AttachDbFilename=|DataDirectory|'LSK.Packets.mdf;Initial Catalog=LSK.Packets;Integrated Security=True;MultipleActiveResultSets=True" providerName="System.Data.SqlClient"
http://www.asp.net/mvc/overview/getting-started/introduction/creating-a-connection-string