ASP.NET MVC identification tables creation - asp.net

So I have a project with my custom, local DB (created by model-first approach). I need to create there identification tables (like asp_users, asp_roles, I don't remember how exactly they are called, but I hope you got the idea). As far as I know, they should be created on first registration, however it doesn't happen:
I can't see those tables in SQL Management Studio either.
My connection string (created automatically with DB):
<!--<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-WebApplication1-20170504122316.mdf;Initial Catalog=aspnet-WebApplication1-20170504122316;Integrated Security=True" providerName="System.Data.SqlClient" />-->
<add name="DefaultConnection" connectionString="metadata=res://*/DBModel.csdl|res://*/DBModel.ssdl|res://*/DBModel.msl;provider=System.Data.SqlClient;provider connection string="data source=VADIM-PC;initial catalog=PIT_3_Project_DB;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
The first connection string is truly default, the second is mine.
The fun part is - authentication works fine, I can register and login, and I don't see where my code cane use with the default local DB.
UPD: Sorry, I haven't changed this row:
public ApplicationDbContext()
: base("DBModelContainer", throwIfV1Schema: false)
{
}
I have changed DBModelContainer to DefaultConnection and now I am getting this error (when I am trying to register)
The entity type ApplicationUser is not part of the model for the current context.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

You should create these tables with code first migrations.
First step:
Uncomment first default connection string,rename it for example DefaultAuthConnection and modify it like this (for local DB).
<add name="DefaultAuthConnection" connectionString="data source=.;initial catalog=PIT_3_Project_DB;persist security info=True;" providerName="System.Data.SqlClient" />
<add name="DefaultConnection" connectionString="metadata=res://*/DBModel.csdl|res://*/DBModel.ssdl|res://*/DBModel.msl;provider=System.Data.SqlClient;provider connection string="data source=VADIM-PC;initial catalog=PIT_3_Project_DB;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
Second Step: Open your IdentityModels.cs and modify ApplicationDBContext like this:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultAuthConnection", throwIfV1Schema: false)
{
}
}
Third Step: Open Package Manager Console (View->Other Windows->Package Manager Console) and write follow steps:
Enable-Migrations -ContextTypeName
WebApplication1.ApplicationDbContext
Add-Migration Init
Update-Database
From now on, you can see authentication tables in your local database.

Related

Unable to complete operation. The supplied SqlConnection does not specify an initial catalog or AttachDBFileName

I am trying to create an App Service web app in the Azure Portal and to connect the web app to my local on-premises SQL Server database using the new Hybrid Connection feature.
Created a simple ASP.NET application using Visual Studio 2015 and trying to connect to the SQL server Database which is on on-prem. And modified the connection String as follows
<connectionStrings>
<add name="DefaultConnection"
connectionString="Data Source=PRAVEEN,1433; User ID=sa; Password=my_password;"
providerName="System.Data.SqlClient" />
</connectionStrings>
When the application is started its running without any errors as shown in below screenshot1.
But when I try register (enter any record) it throws me with an error as specified in the below Screenshot3....
"https://azure.microsoft.com/en-us/documentation/articles/web-sites-hybrid-connection-connect-on-premises-sql-server/" - this is the documentation to which I'm referring to..
So, can anyone from the other end help me out please...
Microsoft SQL Server Management Studio screenshot....
Thank you..
#Praveen,
It is what it says - meaning Initial Catalog is missing in your connection string. It requires the Db name to connect to. You have the servername, SQLUserName, SQLPassword, but missing the DatabaseName it requires. Please change your connection string to include the the DbName as follows
<connectionStrings>
<add name="DefaultConnection"
connectionString="Data Source=YourServerName,1433;Database="YourDbName" UserID=YourId;Password=YourPswd;"
providerName="System.Data.SqlClient" />
</connectionStrings>
Look at this site to configure your connection string appropriately: http://www.connectionstrings.com/store-connection-string-in-webconfig/

How to set a proper connection string for Azure database?

I have an azure website and database. I'm running an ASP.NET MVC 4 / EF 5 app localy and trying to put some data to the azure database before to deploy the app. But I have a TargetInvocationException : {"The ASP.NET Simple Membership database could not be initialized. For more information, please see http://go.microsoft.com/fwlink/?LinkId=256588"}
Keyword not supported : server
This is the connection string that I get from my azure dashboard :
<add name="myconstring" connectionString=Server=tcp:myserver.database.windows.net,1433;Database=mydatabase;User ID=cdptest#myserver;Password=******;Trusted_Connection=False;Encrypt=True;Connection Timeout=30;" providerName="System.Data.EntityClient" />
I tried this I got "Keyword not supported : data source"
<add name="myconstring" connectionString="Data Source=myserver.database.windows.net;Initilal Catalog=mydatabase;User ID=cdptest#myserver;Password=*****;Trusted_Connection=False;Encrypt=True;Connection Timeout=30;" providerName="System.Data.EntityClient"/>
The following works for me:
<add name="coupdpoAPEkowswAEntities" connectionString="Server=tcp:myserver.database.windows.net,1433;Database=mydatabase;User ID=cdptest#myserver;Password=*****;Trusted_Connection=False;Encrypt=True;Connection Timeout=30;" providerName="System.Data.EntityClient"/>
Use Server instead of Data Source, and specify the protocol and the port, and use Database instead of Initial Catalog
OK the providername was wrong. System.Data.SqlClient was excepted as this is a code first app model.
I probably got System.Data.EntityClient from another app with Model First or Database First...

The entity type is not part of the model for the current context

My ASP.NET application is working locally, but when I deploy it, I get the following error: "The entity type Usuario is not part of the model for the current context". I've tried every solution I found, but I couldn't get it work.
I think it can be something wrong in connection string because they are different ones to local database and remote database. This is the connection string in Web.Release.config file:
<add name="ModelContainer" connectionString="metadata=res://*/;provider=System.Data.SqlClient;provider connection string="Data Source = tcp:ebmzszqsw8.database.windows.net,1433; Initial Catalog = {database_name}; User ID = {my_id}; Password={my_password};Trusted_Connection=False;Encrypt=True;Connection Timeout=30"" providerName="System.Data.EntityClient" xdt:Transform="SetAttributes" xdt:Locator="Match(name)" />

Why is the SPA application not creating a database?

I have followed a microsoft video trying to make the single page application and it doesnt create or connect to my sql instance even though I have other applications using code first. For example the connection string was...
<add name="DefaultConnection" connectionString="Data Source=Localhost;Initial Catalog=aspnet-MvcApplication2-201265113324;Integrated Security=True" providerName="System.Data.SqlClient" />
But I get an error saying
A network-related or instance-specific error occurred while
establishing a connection to SQL Server. The server was not found or
was not accessible. Verify that the instance name is correct and that
SQL Server is configured to allow remote connections. (provider: SQL
Network Interfaces, error: 26 - Error Locating Server/Instance
Specified)
Its really strange because my other application with the connectionstring...
<add name="PteDotNetContext" providerName="System.Data.SqlClient" connectionString="Server=Localhost;Database=PteDotNet;Trusted_Connection=true;" />
Works fine and will connect no problem...
If you do not provide a connection string or connection string name to the DbContext constructor, it will try to find a connection name that matches the (inherited) DbContext class name.
Hence you can either change the name of the connection string
<add name="MyDbContext" connectionString="Data Source=Localhost;Initial Catalog=aspnet-MvcApplication2-201265113324;Integrated Security=True" providerName="System.Data.SqlClient" />
Or provide the name of the connection string in the constructor
public class MyDbContext : DbContext
{
public MyDbContext() : base("DefaultConnection") {}
}

Database Connection problem with MVC3

I am using MVC3 with Code first approach. In this case, I had to generate my entity classes from the existing databse.
The database was
Database1.mdf
Once I did that, it created DBEntities and added a new connectionstring in my Web.config which looked something like this:
<add name="DATABASE1Entities" connectionString="metadata=res://*/Models.Task.csdl|res://*/Models.Task.ssdl|res://*/Models.Task.msl;provider=System.Data.SqlClient;provider connection string="data source=.\SQLEXPRESS;attachdbfilename=|DataDirectory|\DATABASE1.MDF;integrated security=True;user instance=True;multipleactiveresultsets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
Now I removed DB Entities and came up with my own DB Context class.
and
Now, I am working with the following connectionstring:
<add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|DATABASE1.mdf;User Instance=true" providerName="System.Data.SqlClient" />
Name of my DBcontext class is TaskContext.
I am not sure what happeend afer this. My code works. But it works on some blank database and it does not reflect anydata in database.mdf. If I add something using my controller then I see that thing is added. But it does not get reflected in Databse1.mdf.
It seeems to have created a its own databse. But I do not see andy .sdf or .mdf file created anywhere.... I am not sure what is going on?
Make the name of your connection string same as TaskContext:
<add name="TaskContext"
connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|DATABASE1.mdf;User Instance=true"
providerName="System.Data.SqlClient" />
OR, you can use your favorite connectionStringName by following these steps:
1- When you are creating a TaskContext object, changes it's connectionString:
var cn = WebConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString;
// or
var cn = WebConfigurationManager.ConnectionStrings[0].ConnectionString; // use your connection index instead of 0
var _context = new TaskContext();
_context.Database.Connection.ConnectionString = cn;
2- in your context class (TaskContext), you should tell to modelBuilder to
remove IncludeMetadataConvention
by this code:
protected override void OnModelCreating(DbModelBuilder modelBuilder) {
modelBuilder.Conventions.Remove<IncludeMetadataConvention>();
base.OnModelCreating(modelBuilder);
}
Try making the name of your connection string and DBContext the same. I believe reading at some point that EF Code First employs a lot of conventions so if it doesn't find a connection string named as the DB Context (in your case "TaskContext") it will try to connect to a SQLExpress (whether or not there is one installed) and will try to find or create the database.
I'm assuming you've got SQLExpress installed. What's down in C:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\? (I think that's the path) Have you got a DB sitting there for this project? Maybe something like [YourNamespace].TaskContext.dbf?

Resources