CREATE DATABASE实体框架失败:系统找不到指定的文件 [英] CREATE DATABASE failed with entity framework: The system can not find the specified file

查看:1844
本文介绍了CREATE DATABASE实体框架失败:系统找不到指定的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到这个错误:

 类型'System.Data.SqlClient.SqlException'的异常出现在EntityFramework.dll但在用户代码

附加信息处理:目录查找的文件C:\pub\LSK\Dev\src\LSK.Services\LSK.Services\ App_Data\LSK.Packets.mdf失败,操作系统错误2(系统找不到指定的文件)。

CREATE DATABASE失败。列出的某些文件名不能创建。检查相关的错误。



使用(VAR上下文=新LDTContext())
{
context.Packets.AddRange(包); //这里出现异常
context.SaveChanges();
}


公共类LDTContext:的DbContext
{
公共LDTContext()
:基地(NAME = LDTContext)
{
Configuration.LazyLoadingEnabled = TRUE;
Configuration.ProxyCreationEnabled = TRUE;
Configuration.ValidateOnSaveEnabled = TRUE;
Configuration.AutoDetectChangesEnabled = TRUE;
Configuration.UseDatabaseNullSemantics = FALSE;
}


公共DbSet<数据包和GT;包{搞定;组; }

}

在我的app.config:

 <添加名称=LDTContext的connectionString =服务器=(的LocalDB)\LSK;初始目录= LSK.Packets;集成安全性=真; AttachDBFilename = | DataDirectory目录| \LSK.Packets.mdf的providerName =System.Data.SqlClient的/> 



为什么当数据推入表的数据库不是第一次的AddRange()创建<? / p>

解决方案

您连接字符串是错误的[数据源=(的LocalDB)\v11.0不是服务器=(的LocalDB)\LSK ]。尝试

 的connectionString =数据源=(的LocalDB)\v11.0; AttachDbFilename = | DataDirectory目录| \LSK.Packets密度纤维板;初始目录= LSK.Packets;集成安全性= TRUE; MultipleActiveResultSets = TRUE的providerName =System.Data.SqlClient的

的http:// www.asp.net/mvc/overview/getting-started/introduction/creating-a-connection-string


I get this error:

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

    }

In my 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" />

Why is the database not created on the first AddRange() when data is pushed into the tables?

解决方案

You connection string is wrong [Data Source=(LocalDb)\v11.0 not Server=(LocalDb)\LSK]. Try

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

这篇关于CREATE DATABASE实体框架失败:系统找不到指定的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆