简体   繁体   中英

How I tell my project to use the database in project folder

I created database with ef code first. I copied the 2 files that ef created to the project folder DAL.UsersDb.mdf and DAL.UsersDb_log.ldf

Now I want to tell the project to use the new location. I added them to config file but its not working

<add name="UsersDb"
   connectionString="Data Source=(LocalDB)\v11.0;
   initial catalog=UsersDb;
             AttachDBFilename=|C:\Users\HOME\Desktop\MyProject|\DAL.Funcs+DataBase.mdf";
             integrated security=True;  MultipleActiveResultSets=True;  App=EntityFramework"
   providerName="System.Data.SqlClient" />

I got the following Exception:

Cannot open database \\"DAL.UsersDb\\" requested by the login. The login failed.\\r\\nLogin failed for user 'HOME\\598'.

public class UsersDb : DbContext
{
    public DbSet<BE.User> users { get; set; }
}

Set the user on your application pool to "Local System". It has admin rights, it might be able to open the DB.

may be the service user not have access to database, for testing you can pass user name and password in connection string

<add name="UsersDb"
       connectionString="Data Source=(LocalDB)\v11.0;
       initial catalog=UsersDb;
                 AttachDbFilename=|DataDirectory|C:\Users\X7\Desktop\MyProject\DAL.UsersDb.mdf;
                 user id=user;password=pass;  MultipleActiveResultSets=True;  App=EntityFramework"
       providerName="System.Data.SqlClient" />

And in deployment you can save the username and password in code and change connection string in code

code supply the password to a connection string

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM