Entity Framework 6 with Azure DB secondary connection - sql

I'm facing a problem with EF against Azure DB secondary connection. Here is my problem:
I'm using EF 6 with code first
I already setup a primary Azure DB with 2 Active GEO-Replicated secondary database (have difference server and db name as well as login account)
I use EF6 to manipulate the primary Azure DB. The case is that if the primary DB is down (by any reason) and I want to switch to the active secondary dbs for select statements at runtime. How can I achieved that?
Notes: For example, I have the code snippet below:
using (AppContext context = new AppContext()) // AppContext is derived from DbContext and using the primary connection string
{
var users = await context.Users.ToListAsync(); // If this line of code failed with the primary connection string
}
I want to auto-switch dynamically to the secondary db if the line var users... is failed at runtime. Any recommendation?
Thanks for your help!

Related

How to automatically transfer data from Cosmos DB to Azure SQL database?

I would like to sample relevant data from Cosmos DB documents every time Cosmos DB receives a new document, and send it automatically to Azure SQL Database.
The whole purpose is to create a live Power BI report that gets updated with the newest data that comes in to Cosmos DB, but since I don't need to show everything, I made a SQL Database in Azure and I am only missing how to make an Azure function that is triggered when Cosmos DB receives a new document. Also that the function needs to get the relevant data and sends it to Azure SQL.
Is there a standard way to do this?
Do I have to make an Azure Function App?
I am very new to both coding and Azure, so I appreciate if someone can help using a beginner language.
However, any help is appreciated.
You can easily do that with an Azure Function that uses the CosmosDB Trigger.
The trigger will be, well, triggered whenever there is one of more changes or and additions in the CosmosDB collection that you are targeting. Then simply add your own code for SQL insertion.
namespace CosmosDBSamplesV2
{
public static class CosmosTrigger
{
[FunctionName("CosmosTrigger")]
public static void Run([CosmosDBTrigger(
databaseName: "CosmosDBName",
collectionName: "CosmosColName",
ConnectionStringSetting = "CosmosDBConnection",
LeaseCollectionName = "leases",
CreateLeaseCollectionIfNotExists = true)]IReadOnlyList<Document> documents,
ILogger log)
{
if (documents != null && documents.Count > 0)
{
//do SQL insert with your code here
}
}
}
}
You can read how you can connect to a SQL db from an azure function here: https://learn.microsoft.com/en-us/azure/azure-functions/functions-scenario-database-table-cleanup
You can read more about CosmosDB related azure functions here: https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-cosmosdb-v2

SubmitChanges() updates database in bin folder

My code and the Linq to sql function SubmitChanges are working, but when using a local database a copy of the database in the bin folder is updated and not the primary database. So the changes aren't shown on a new query. If I re-connect the database but don't load it as local same problem - the primary database isn't updated, but now I can't figure out which one is (tks to this question).
What setting for a local db or how do I use a non-local database to show changes on a new query of the database?
Dim DATA As New lnqPolarisDataContext
Dim newBOOK As New BOOK()
newBOOK.ID = 14
newBOOK.LEG = 11
newBOOK.P_C = "C"
newBOOK.STRATEGY = "STRADDLE"
newBOOK.STRIKE = 999
newBOOK.CONTRACT = "XXX"
DATA.BOOKs.InsertOnSubmit(newBOOK)
DATA.SubmitChanges()
... new query doesn't show these changes
maybe this is the best method?
The real solution in my opinion would be to put your database on the server where it belongs - after all, SQL Server is a server-based solution, not a file-based "database".....
install SQL Server Express (and you've already done that anyway)
install SQL Server Management Studio Express
create your database in SSMS Express, give it a logical name (e.g. YourDatabase)
connect to it using its logical database name (given when you create it on the server) - and don't mess around with physical database files and user instances. In that case, your connection string would be something like:
Data Source=.\\SQLEXPRESS;Database=YourDatabase;Integrated Security=True
and everything else is exactly the same as before...
Also see Aaron Bertrand's excellent blog post Bad habits to kick: using AttachDbFileName for more background info.

MVC 4.0 simple membership database

I am aware of old membership and familiar with aspnet_regsql.exe concept. Now I'm using asp.net MVC4.0 (Visual Studio 2013). How do I get new simple membership table of MVC 4.0 in SQL server management studio.
The tables are created automatically the first time you try to register a user.
so if you want to access those table via SQL server,you need create one database in sql server and replace default connection string in web.config with (new database) connection string.
so now membership table will created in your new database
See this link.
if I understand you correctly, you need to using InitializeSimpleMembershipAttribute
where most interest is the following code
WebSecurity.InitializeDatabaseConnection("EFDBContext", "UsersTable", "UserId", "Login", autoCreateTables: true);
Where:
- EFDBContext (connection string name)
after which all the necessary tables will Create, in particular
webpages_Membership
webpages_Roles
webpages_UsersInRoles

Linq or SQL join tables on different servers

I have just started working for a new company where I am regularly having to query 8 different servers (all Microsoft SQL 2005 servers). At the moment every morning I am connecting to each server individualy every morning to read the latest entry in a [application].[tmpLogs] table. I have to use different users names and password for each server. I want to write a Linq or SQL query that I can just run in LinqPad to get the last row entered in the tmpLogs table on each server. Does anyone know how I can connect to all the servers and query the tables in one query?
Do the tmpLogs tables have the same columns on each server? If so, you can take advantage of the fact that LINQPad lets you create new typed DataContexts with different connection strings.
Just connect to just one of your databases, and do this:
// Dump the log for the current database:
TmpLogs.OrderByDescending (l => l.Date).First().Dump();
// Dump the logs for other databases:
string[] otherConnectionStrings =
{
"server=...database=....etc",
"server=...database=....etc",
...
}
foreach (string cxString in otherConnectionStrings)
{
var dc = new TypedDataContext (cxString);
dc.TmpLogs.OrderByDescending (l => l.Date).First().Dump();
}
You could choose one SQL Server as your 'master' server. Then set up the other servers as LINKED Servers (see http://msdn.microsoft.com/en-us/library/ms188279.aspx). You could configure a LINQ to SQL object to connect to the LINKED servers via the 'master' server.
You could also just take this out of LINQ and set up scheduled tasks to push the data into a 'warehouse' table periodically. It's easier to communicate with LINKED servers via Stored Procedures than it is via LINQ. As far as I know, LINQ doesn't contain the concept of DB catalogs, only tables. The catalogs are abstracted out in the DataContext object, which if you're using Linqpad, doesn't exist.

Multiple Versions of SQL Server using Entity Framework in a single ASP.NET application

I am using the Entity Framework in a web application that utilizes SQL server 2000, 2005, and 2008. When I create a new EDMX file using anything other than 2008 (version of the first edmx created) I receive error 0172: All SSDL artifacts must target the same provider. The Provider 'MyDatabase' is different from ' MyDatabase ' that was encountered earlier. It seems that somewhere in the code the connection is wired to a 2008 datastore and when it checks the SSDL file and sees a different ProviderManifestToken value it throws this error. I am a little more than frustrated. It is hard to imagine that EF will only work with a single version of Sql Server per application. I am fairly sure there must be a setting or workaround. Does anyone have a solution to use different versions of SQL server and the Entity Framework within a single web application?
I was able to accomplish this by putting each edmx in a separate assembly. Then in the connection string, replace all the res://*/... with res://NameOfAssembly/...
I can even perform joins between the two entity models (contrary to claims I found in other sources), e.g.:
var oneDb = new Entities2000();
var otherDb = new Entities2005();
var results = from one in oneDb.SomeSet
join other in otherDb.OtherSet
on one.Property equals other.Property
select new {
SomeProp = one.SomeProp,
OtherProp = other.OtherProp
};
This link helped me to solve the problem when there was a difference in SQL server 2005 and 2008.
http://kkryczka.wordpress.com/2011/01/03/all-ssdl-artifacts-must-target-the-same-provider-the-providermanifesttoken-2008-is-different-from-2005-that-was-encountered-earlier/
Right click on the .edmx file and select Open with XML Editor.
Open Entity Framework .edmx file:
Change the ProviderManifestToken to 2008:
Looks like its a known issue for Microsoft.