How connect to existing MSSQL database on server from local computer with .net core 3 library Project - orm

I am trying to connect to SQL Server in a .NET Core 3 library, for implementation this ORM I use "Database First Scaffold-DbContext" command like this:
Scaffold-DbContext "Server=IP/sql2016;Initial Catalog=DatabaseName;
Persist Security Info=True; User ID=UserName; Password=Password;
MultipleActiveResultSets=False; Encrypt=True; TrustServerCertificate=True;
Connection Timeout=30;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir
Models -Force
If I use this command with IP server, it just creates DbContext and does not create table models. If I replace the server IP with local (when restore database backup to my local database) it creates all data models.
I don't know what should I do?
Another question: when I create a data model, does it not assign primary key to my model?

My database user was not db owner so it can't create models.

Related

How to move EF migration and update database in Azure SQL DB

I have an Entity framework project created in Visual Studio 2019.
I have created a migration using Add-migration, and have updated the local SQL DB using Update-database. The changes in migration are reflected in (localdb)\MSSQLLocalDB. Everything is working as it should.
Now I want to publish my application from VS, and want the changes in my migration and local DB to move/migrate to the Azure SQL DB for use by masses.
How do I make sure after publishing, the changes have migrated to the Azure SQL DB?
Thanks,
KB
If you have access to azure database from your local just do Update-Database with azure database connection string. otherwise, you should do this somewhere in your application:
yourDbContext.Database.Migrate();

Creation and Using of a database during runtime in .NET Core

I have a .NET Core backend that is currently connected to a database via the connection string from appsettings.json. Through a migration, the database can also be created or extended.
Now I have a requirement that the software should connect to additional databases with the same DbContext at runtime and if they don't exist yet create them via migration. Thereby the backend should be restarted. The user should be able to choose via the frontend which database he wants to connect to or if he wants to create a new one.
At the moment I am not clear how to implement this. I hope that one of you has a good idea.

I have a local SQL Server Data base. It's working in local. I am trying to pass from Local database to Azure.It's not working using .net

I have a local SQL Server Data base. It's working in local. I am trying to pass from Local database to Azure and it is not working.
My local connection code is as follows:
add name="TestConnectionString" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=testdb;Integrated Security=True;Pooling=False" providerName="System.Data.SqlClient"
Here is the Azure connection code that is not working:
add name="TestConnectionString" connectionString="Server=tcp:.\SQLEXPRESS;Database=testdb; Connection Timeout=30; Encrypt=True; TrustServerCertificate=False;"
How can I solve this connection problem?
You can't use SQL Express once you deploy to Azure.
The data storage possibilities for your application running in Azure Web Sites are: Azure SQL Database, Azure Blob Storage, Azure Table Storage, MySQL database ...
If you want you can use SQL Compact edition on azure id you don't want to pay for SQL Azure:
You can install: SqlServerCompact nuget package.
Your SQL Compact Edition connection string will look like this:
<add name="DefaultConnection" connectionString="Data Source=|DataDirectory|\TwoNotes.mdf"
providerName="System.Data.SqlServerCe.4.0" />
Here is an article on how to do that:
http://www.dotnetcurry.com/windows-azure/883/host-aspnet-mvc-azure-without-database
The connection string needs to be modified to the Azure SQL DB, since Express and SQLDB are different backends.
An Azure SQL DB connection string should look like this:
Server=tcp:xxxxxxxxxx.database.windows.net;Database=testDB;User ID=MyAdmin#xxxxxxxxxx;Password=pass#word1;Trusted_Connection=False;Encrypt=True;
You can obtain this connection string from the Azure Portal. This article contains additional information on how to create a database and connect to it from ASP.NET: https://azure.microsoft.com/en-us/documentation/articles/sql-database-dotnet-how-to-use/
Luis

SQL Server database is not visible

I have installed ASP.NET application with database on our server. ASP.NET application created database using connection string below. The problem is that I do not see database in SQL Server Management Studio. I use "Windows Authentication" account to login to SQL Server. How to solve this problem? Also I see two sqlservr.exe instances in process manager.
server=(local)\SQLEXPRESS;database=bugs;Integrated Security=True;Connect Timeout=30;User Instance=True
You won't see databases created with "User Instance=True" unless you login as the exact user the database was created under, and the database has been attached.
Given that if ASP.NET created the database it's likely it was created by Network Service then you are not going to see it at all. Whilst you could manually try to attached it as a user instance yourself, you may well end up messing up the permissions or hitting the "database already exists" problem.
Make sure you're connecting to (local)\SQLEXPRESS instance and not to the default instance.
Where is the stored procedure that created the database? Just because there is a connection string does not guarantee that there was a database created.

aspnet_regsql.exe and MVC

Can someone explain what aspnet_regsql.exe is really used for?
When I create a standard ASP.NET MVC project in VS2008 and register a user, I get the db created with corresponding asp.net membership tables etc.
This uses the SQL Server Express 2005 as standard.
The forums I have found states that aspnet_regsql.exe is used when one is migrating the created SQL Server Express db to a SQL Server, fx. to a hosting server. I am right about this?
But is this change a global change, meaning that next time I start a new MVC project or standard Win Forms project and add a db, it will use SQL Server as provider and not SQL Server Express? Or is this aspnet_regsql.exe only used on a project basis?
Like you say, the aspnet-regsql will create the tables into any (Express or not) version of SQL Server. I -think- your main question is how do you know which database you're attaching to, right? If so, then that is handled in your web.config file in the "Membership" provider area. What I do is use a SqlMembershipProvider for my development (utilizing the tables created with aspnet_reqsql) and then switch to ActiveDirectoryMembershipProvider for production.
MSDN Article on aspnet_reqsql
aspnet_regsql.exe is a utility designed to prepare a database to work with asp.net providers. These providers provide services to asp.net applications such as user membership, roles, and profile management and require a database that adhere to specific schemas.
By running this application you can alter an existing database to adhere to these schemas or create the default database for these services (aspnetdb.mdf?).
I believe that the utility might also prepare some of the intrinsics for provider usage.