How to add new table in sql server - c#

I already created a Asp.net core web api project and have created a database using identity with migration. Now I need to know, that can I add new table to the database and how can i do that? I'm new to this field.
I also have tried Add-Migration but giving me error. I have no idea about this.
The error message is:
An error occurred while accessing the IWebHost on class 'Program'.
Continuing without the application service provider. Error:
AddEntityFrameworkStores can only be called with a user that derives
from IdentityUser. More than one DbContext was found. Specify
which one to use. Use the '-Context' parameter for PowerShell commands
and the '--context' parameter for dotnet commands.

Create your model and add in DB context.
public DbSet<UserAttachment> UserAttachment {get; set;}
After that
add-migration
And then
update-database

Related

Choosing DB context when adding identity to existing project

I'm trying to add Identity to an existing and working MVC project.
When used this command Add-Migration CreateIdentitySchema I got error message: 'More than one DbContext was found. Specify which one to use. Use the '-Context' parameter for PowerShell commands and the '--context' parameter for dotnet commands.' and I chose the existing DB context that I have previously created for the main functionality of the application and now it shows 'There is already an object named 'ExpenseReport' in the database.' so it's trying to recreate my initial model.
Should I have a separate DB Context for Identity and if yes how to connect user to data from the other context?
So, i think you can use several DbContexts with different connections (just need to write it in your Startup class) or you need to use pattern "DbContext Factory". You can read more here .
Hope it will help you.

Asp.net Identity User, Adding First Name and Last Name

I have used this link to add first name and last name. Now I'm seeing an error Cannot drop database "dbname" because it is currently in use..
I don't want to drop my Database, just want to add first name and last name in register of asp.net identity user in mvc5. give me suggestions..
DropCreateDatabaseAlways I was using this option. After changing to
Database.SetInitializer<ApplicationDbContext>(new
MigrateDatabaseToLatestVersion<ApplicationDbContext,
APPLICATION.Migrations.Configuration>());
This works fine, also I need to enable migration using package manager console
Enable-Migrations
Add-Migration "FirststName"
Add-Migration "LastName"
Thanks #Archana Parmar

How do I resolve my Invalid object name 'dbo.AspNetUsers' error?

I changed my connection string when starting a new mvc project because I needed to use some info out of an existing database. Now I get a "Invalid object name 'dbo.AspNetUsers' when I try to load the startup page. A quick google search says the issue happens because you are connecting to an existing database using asp.net users. Still, this should not occur since I am not using code first migrations, right? What is more, I am using Active Directory to authenticate my users instead of individual user accounts.
In addition, the AspNetUserstable does not exist in the database I am calling in the web.config:
Due to the fact that I am not using the UserStore or the ApplicationUser provided by the Individual User Accounts defaults I needed to change this:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
to
public class ApplicationDbContext : DbContext
Optionally If you are using Code First Migrations you could add the initial migrations, remove all of the sql code from the up and down methods, and finally update the database. Even though the tables will still not exist it seems to satisfy the compiler and your project as the error goes away and the page loads as expected.

Rename ASP.Net Identity 2.0 Tables: Context has changed since the database was created

I already have my database and I need to use ASP.Net Identity 2.0. So I did the following:
Get the SQL Script of identity tables:
Create an MVC Project with Authentication Individual User Account
Then, script the database that get created under Default Connection
Then, I intergrated it with my existing DB:
Insert the scripted tables into existing database in SQL Server
Management Studio.
After I generated My Identity Database, i started added the necessary columns to the table "ASPNetUsers" by using EF Migrations for DB First.
I created a new MVC Project using Database First Approach
Now, i have 2 connection strings in web.config
As a result, I have the Asp.Identity Tables in my db with ER model in my application.
Then, I started adding new properties:
public class ApplicationUser : IdentityUser
{
public string Name { get; set; }
public virtual Region Region { get; set; }
}
After adding each property, i executed the Migrations commands: Add-Migration and Update-Database.
Then, i tried to register new user with the added columns populated all is working fine.
However, I need to change the name of the Table, so I used Fluent API:
protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<ApplicationUser>().ToTable("Account");
}
when running the app and trying to create a new user it gives me the following error:
It seems you have not updated your model yet.
If you are using Code First please follow the instructions given on
http://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/migrations-and-deployment-with-the-entity-framework-in-an-asp-net-mvc-application
basically execute these commands in the package manager console:
enable-migrations
add-migration InitialCreate
update-database
If you need to do changes after first migration you should execute:
add-migration
update-database

InitializeSimpleMembershipAttribute returns TargetInvocationException

I am still a relatively inexperience with Visual Studios and C# so please let me know if necessary information has not been provided or I am unclear in my description.
I have an MVC 4 project in Visual Studio 2010 Web Developer Express and I am attempting to set up basic Forms Authentication. I have used the ASP.NET Configuration Manager to set up a couple test users and roles. The Configuration Manager has also created the ASPNETDB.MDF database in my App_data folder which has been included in the project.
I am currently using the generated AccountController and Account views.
Whenever I attempt to access a HTTPGET or HTTPPOST method with which has the [Authorize] attribute, a TargetInvocationException is thrown from the following method in the InitializeSimpleMembershipAttribute class:
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
// Ensure ASP.NET Simple Membership is initialized only once per app start
LazyInitializer.EnsureInitialized(ref _initializer, ref _isInitialized, ref _initializerLock);
}
When viewing the details of the exception, the inner exception states that "The ASP.NET Simple Membership Database could not be initialized."
The inner exception of the above inner exception states that it "Could not find the conceptual model type for" a given model.
The model that was not found belongs to an ADO.Net [.edmx] file which is generated from a database. The model that it cannot find is alphabetically the first table/class in the database, so it likely also applies for every class from that database.
I am not sure if it is important, but I use a database separate from the ASPNETDB.MDF to store information enter on the website.
I do not understand why the LazyInitializer.EnsureInitialized() method may be causing this problem.
Have you checked the connection string used by WebSecurity.InitializeDatabaseConnection in Global.asax.cs Application_Start() - this could potentially cause the issue.
If the named connections string is correct are you sure the dbContext connection string is correct:
public SomeContext(): base("MyConnectionStringName")
{
}

Categories

Resources