CREATE DATABASE failed with entity framework: The system can not find the specified file

£可爱£侵袭症+ 提交于 2019-12-08 11:15:18

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/30510186/create-database-failed-with-entity-framework-the-system-can-not-find-the-specif

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!