0

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?

Pascal
  • 12,265
  • 25
  • 103
  • 195
  • Does the path exist (C:\pub\LSK\Dev\src\LSK.Services\LSK.Services\App_Data)? Do you have write access? Is the file LSK.Packets.mdf already there? Try moving it out and repeat. – Steve Greene May 28 '15 at 15:10
  • well App_Data does not exist only until LSK.Services\ – Pascal May 28 '15 at 15:42

1 Answers1

0

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

Steve Greene
  • 12,029
  • 1
  • 33
  • 54