Merging MVC4 membership database with entity framework code first database - sql-server

I am using MVC4 with the default membership provider and using entity framework code first. The membership data is in a database stored in my app_data folder called aspnet-xxxxxx.mdf, and my application data is created in a separate database running on SQL Server express.
What do I have to do so that I have 1 database containing my membership tables and the application data? I do not mind losing the data in either of these databases as at the moment it is only test data.

Find your InitializeSimpleMembershipAttribute.cs and change in the line
WebSecurity.InitializeDatabaseConnection("[ConnectionString]", builder.Provider, "UserProfile", "UserId", "UserName", autoCreateTables: false);
[ConnectionString] to your connection string name you defined in Web.config. Or provide a complete connection string

Related

Recreate database when running ASP.NET Core Web API on another server

I have a project that is an ASP.NET Core Web API using Entity Framework Core.
A few time ago I created the database using code-first approach with the necessary migrations.
Now I want run the project in other machine and I get the error when I try for example an post to one endpoint:
Microsoft.Data.SqlClient.SqlException (0x80131904): Cannot open database "MyDatabase" requested by the login. The login failed.
However I think the problem (and I checked with ssms) is that the database doesn't exist.
My connection string, in DbContext is:
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(#"Server=(localdb)\mssqllocaldb;Database=MyDatabase;Trusted_Connection=True;");
}
What is the best way to recreate the database on this machine?
Thanks
Remove
;Database=MyDatabase
From the connection string.
Then you can create the dtaabase
CREATE DaTABASE MyDatabase
After the command has run
USE MyDatabase
so that you can create tables and run sql commands on it.
This will wrk on any database.
You should check prior to the creation if the database exists, bu that is highly depending on the rdms that you are using

Should we delete physical database for the deleted clients in SAAS application

I am working on a project which is in .net core and hosted on azure and being used as SAAS application by different clients all over the world having separate databases.
Now I got a requirement which says we should delete the databases of those clients which are no longer in master db and deleted by admin.
I am in doubt that should we really delete the database for the clients which would be getting deleted?
Also if yes is it feasible to delete the database through application or we should use some utility to do that?
Also I am afraid if server will allow application to delete the database?
Can any one please suggest me on this?
Following tech stack i am using:
.net core (backend) with EF core
MSSQL (database)
Theres a few different ways.. Essentially it works the same as any other database, so you can execute T-SQL to do it.. or use the Azure SDK.. Here is some examples of different approaches: https://learn.microsoft.com/en-us/sql/t-sql/statements/drop-database-transact-sql?view=sql-server-2017
You can execute raw SQL statements in EF like so:
using(var context = new SampleContext())
{
var commandText = "INSERT Categories (CategoryName) VALUES (#CategoryName)";
var name = new SqlParameter("#CategoryName", "Test");
context.Database.ExecuteSqlCommand(commandText, name);
}
So, using that format, you could add your DROP Database command in? Taken from: https://www.learnentityframeworkcore.com/raw-sql - under the Database.ExecuteCommand section

How can i solve a "Model compatibility cannot be checked because the database does not contain model metadata" error?

I’m trying to connect to a remote server from within my project using this connection string :
Data Source=myServer.com,14330;Initial Catalog=myDatabase;Persist Security Info=True;User username;Password=password"
providerName="System.Data.SqlClient" />
I got this error message:
Model compatibility cannot be checked because the database does not contain model metadata. Model compatibility can only be checked for databases created using Code First or Code First Migrations.
If my remote database, SQL 2008 R2, does not have a metadata model, should I create one to make it compatible with the metadata of my local database during development?
I removed references to the initializer class that I used to seed my local database.

SimpleMembership with SQL Server Express database

I have recently discovered that ASP.NET MVC 4 uses SimpleMembership rather than the traditional ASP.NET Membership provider.
I am looking to use SimpleMembership but I also wish to use an Sql Server express database (I will be using a 'real' Sql Server db later on).
I am used to using aspnet_regsql to setup my sql server databases for application services and therefore inserting the necessary membership tables.
However, this application will not setup a DB to work with SimpleMembership.
Can anyone tell me how to do this?
You will need to comment out the WebSecurity.InitializeDatabaseConnection line of code (otherwise the first time you reach the login form the application will attempt to create the simple membership tables on the defined web.config connection string).
Then you will need to update your account controller code since its was written for simpleMembership use to reflect your chosen membership provider.
Recently I did this by creating an MVC3 project then upgrading it to MVC4 (read the how to upgrade mvc3 to mvc4 section). Not the cleanest but it did prevent me from having to update the account controllers code.
I would recommend to look into InitializeSimpleMembershipAttribute, which is in the folder "Filters"
There is a command to initialize the database. It checks itself, if the tables are already created.
WebSecurity.InitializeDatabaseConnection("connStringName", "UserProfile", "UserId", "UserName", autoCreateTables: true);
The tables are:
UserProfile => in my case, because of the initializer
webpages_OAuthMembership
webpages_Membership
webpages_OAuthToken
webpages_Roles
webpages_UsersInRoles
You can check out the source-code of the aspnetwebstack project

Changing database name for existing Asp.net MVC3 project

I am working on Asp.net MVC 3 project, I need change the database name for the project I tried changing the database name in connection string & What happens is that when I pull the data it pulls from new DB where as when I try to insert data in some table it tries to insert in the old db. I am sure from where it is getting reference to the old DB name. Please HELP.
There must be a different connection string for the inserts. You should make a search in the entier solution after the database name or parts of the connection string to find that connection string and modify it too.

Resources