0
votes

I am trying to change my asp.net mvc project from no authentification, to individual user account. I got to the point where I need to change my AdminDbContext to Application Db context.

I read that i need to put in the base the connection string of my mdf database, which i did, but then i got the error in the title. I tried removing the attachdbfilename but that did not work either because of the security integrated. After I deleted that one it told me that the path is invalid.

public class 
    {

         public ApplicationDbContext()
            : base("Data Source = (LocalDB)\\MSSQLLocalDB; AttachDbFilename=C:\\Users\\Alex\\Desktop\\Licenta\\Licenta\\App_Data\\Database.mdf;Integrated Security 
          = True", throwIfV1Schema: false)
        {    
        }

        public DbSet<Food> Foods { get; set; }
        public DbSet<User> Users { get; set; }

        public static ApplicationDbContext Create()
        {
            return new ApplicationDbContext();
        }
    }

The exception that I get is from this code:

 if (!roleManager.RoleExists("Administrator"))  <---
            {
                // Se adauga rolul de administrator
                var role = new IdentityRole();
                role.Name = "Administrator";
                roleManager.Create(role);
                // se adauga utilizatorul administrator
                var user = new ApplicationUser();
                user.UserName = "[email protected]";
                user.Email = "[email protected]";
                var adminCreated = UserManager.Create(user, "Administrator1!");
                if (adminCreated.Succeeded)
                {
                    UserManager.AddToRole(user.Id, "Administrator");
                }
            }

It is different from this question: Keyword not supported: 'attachdbfilename' - MDF Database File (C#), because i dont use sqlce and removing the attachdbname still doesnt work as I have mentioned above.

1
This might work. - Erdem Ozdemir
@ErdemOzdemir i edited, it is not that, i still have errors when i remove it. i wrote that in the question. - Alex Staniea
@AhmadMM edited, it is not that, i still have errors when i remove it. i wrote that in the question. - Alex Staniea
I tried removing the attachdbfilename but that did not work either because of the security integrated. After I deleted that one it told me that the path is invalid. - Alex Staniea

1 Answers

0
votes

Use this: AttachDbFilename=|DataDirectory|\dbfilename.mdf

Try setting the User Instance = True in your connection String.

If you still get error. please let me know the error msg