How to change the database in a WCF service connection string? - wcf

In Azure, I have a database called Sismos, this was targeted by my WCF service, I created a copy of this database like this on Azure:
CREATE DATABASE sismos_cfe AS COPY OF Sismos;
This was because the initial database was only a for testing and will be used for other purposes and this new one will handle all the work for this WCF servive.
In my WCF service, I changed the following line in my Web.config file:
<connectionStrings>
<add name="Model1Container" connectionString="metadata=res://*/Sismos.csdl|res://*/Sismos.ssdl|res://*/Sismos.msl;
provider=System.Data.SqlClient;provider connection string="data source=*******.database.windows.net;
initial catalog=Sismos;persist security info=True;user id=*******;password=*****;
multipleactiveresultsets=True;application name=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
With this:
<connectionStrings>
<add name="Model1Container" connectionString="metadata=res://*/Sismos.csdl|res://*/Sismos.ssdl|res://*/Sismos.msl;
provider=System.Data.SqlClient;provider connection string="data source=******.database.windows.net;
initial catalog=sismos_cfe;persist security info=True;user id=*****;password=******;
multipleactiveresultsets=True;application name=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
And this line in my app.cofig file:
<connectionStrings>
<add name="Model1Container" connectionString="metadata=res://*/Sismos.csdl|res://*/Sismos.ssdl|res://*/Sismos.msl;
provider=System.Data.SqlClient;provider connection string="data source=******.database.windows.net;
initial catalog=Sismos;persist security info=True;user id=****;password=*****;
multipleactiveresultsets=True;application name=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
With this:
<connectionStrings>
<add name="Model1Container" connectionString="metadata=res://*/Sismos.csdl|res://*/Sismos.ssdl|res://*/Sismos.msl;provider=System.Data.SqlClient;
provider connection string="data source=******.database.windows.net;
initial catalog=sismos_cfe;persist security info=True;user id=****;password=*****;
multipleactiveresultsets=True;application name=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
As you can see, I only replaced the value of the initial catalog property, instead of pointing to Sismos, it now should point to cfe_sismos.
The problem I'm having is that when I try to access one of my endpoints that deals with deleting an entry in the database, no change is made in the database at all. With operations of selecting entries or editing/inserting an entry there's no problem. With the initial catalog property value eing Sismos there's no problem with any of the endpoints, in theory any database access should be directed to the cfe_sismos database.
Was it not enough with me replacing the initial catalog property value? Any help will be appreciated.
EDIT
I just noticed that there's also problems and odd behaviour when editing an entry, for example, if I have an entry in my table Users and I edit the Last_Name, the change won't be reflected when I check my database on Azure, but if I call the endpoint that returns the users, the change will be present, I gave some time to see if there was some kind of delay preventing from showing the newest values in Azure, but it didn't show any changes. How can I be making changes in the database without those changes being shown in the actual database? If I try to insert a new entry into a table, the entry will be shown with no problem.
So in summary, through my endpoints in my WCF service, if I insert something it will be shown in azure, if I edit something it won't be shown in Azure(but the change will be available when calling the respective endpoint) and if I want to delete something it won't be shown in Azure nor the change will be reflected when calling the endpoint.
If the database in Azure is not receiving any changes, then where am I getting all of the information? It's like a cached database exists somewhere, I'm really confused at the reason for this is happening.
I'm really confused at this odd behaviour, so I hope someone can help me. Thanks in advance.

I finally found the reason for the odd behaviour. The thing is that Entity Framework can't work with objects that are copied from another obtained from the Context object generated by Entity Framework.
What I was doing was storing the data from my entities into static Lists to avoid as many connections to the database as possible, only making connections when an insert, update or delete operations were needed. But when I tried to get an object from those lists and use it to update or delete on the database, since it wasn't obtained from the context, then the operations is invalid. This is why only insert operations were working properly, since those are new objects and can interact with no problem with the context.
So in the end I changed the logic for the update and delete operations on my DAO classes, so instead of doing this:
Clusters cluster = (from c in DatabaseInfoHolder.ListaClusters
where c.ClusterId == model.ClusterId select c).FirstOrDefault();
I went with this:
Clusters cluster = (from c in context.Clusters
where c.ClusterId == model.ClusterId select c).FirstOrDefault();
I was hoping for Entity Framework to recognize the values from the object in my static Lists, but it seems it doesn't work that way.

Your config says "initial catalog=sismos_cfe" but you said "CREATE DATABASE cfe_sismos"
sismos_cfe != cfe_sismos

Related

String or Byte array. error even use of Column Encryption Setting=enabled

I use always encryption in some field with any of master key stores(I was test all of these):
-current user
-local machine
-CNG
even from application side configure this connection string:
<connectionStrings>
<add name="DataContext" connectionString="data source=our-PC\SS2016;initial catalog=testdb;integrated security=True; MultipleActiveResultSets=True;App=EntityFramework;Column Encryption Setting=enabled" providerName="System.Data.SqlClient" />
</connectionStrings>
When running in web and want to load some data using EF by this block of code :
var datas = context.tbUsers.ToList();
Below error occured:
Additional information: The property 'Name' is not a String or Byte
array. Length can only be configured for String and Byte array
properties.
What happened?
you must be careful of using of Enum type
one filed of one of the table property have a enum type which is Prohibited
I Changed the propery and programs run okay

Default SqlConnection.ConnectionTimeout setting in serverwide

For some reasons, I need to set higher duration for default ConnectionTimeout for asp.net to sql connection. Because some sites, during installing database, which has not connection string yet in web.config, need to execute long sql commands to create sql databases. And I need to allow those installs higher ConnectionTimeout value. Where do I do that? note that there is no connection string key in web.config if database is not created. Thats why I need to change default value.
If there is no connection string which you have created then you can change the timeout like this:
cmd.CommandTimeout = 90; //By default the value is 30
Apply this to the place where you are going to execute your query from the code.
If you want to make the above code a bit generic then you can create the below change in the appsettings.config
<appSettings>
<add key="SqlCommandTimeOut" value="90"/>
</appSettings>
and then you can call it like this:
cmd.CommandTimeout = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["SqlCommandTimeOut"]);

Invalid value for key attachdbfilename after renaming connectionString

I am building the sample MvcMovie tutorial for ASP.NET MVC 4. I'm using EntityFramework Code First features and created a connectionString as follows.
<add name="MoveDBContext"
connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDictionary|\Movies2.mdf;Integrated Security=True"
providerName="System.Data.SqlClient"
/>
Everything worked fine at this point. But then I realized that I named my connection string MoveDBContext instead of MovieDBContext and, being the perfectionist I renamed it. After doing this I now receive an error in my MoviesController/Index method.
public class MoviesController : Controller
{
private MovieDBContext db = new MovieDBContext();
public ActionResult Index()
{
return View(db.Movies.ToList()); // Error: Invalid value for key 'attachdbfilename'
}
...
}
If I change the name back to MoveDBContext the error goes away.
Can anyone tell me where this original name is being referenced?
EF, by default, looks for a connection string with the same name as the type that extends DbContext.
Or, better put by Scott:
By default, when you create a DbContext class with EF code-first, it
will look for a connection-string that matches the name of the
context-class. Since we named our context class “NerdDinners”, it
will by default look for and use the above “NerdDinners” database
connection-string when it is instantiated within our ASP.NET
application.
Edit:
After looking closer, I think your connection string is the problem. You've got DataDictionary instead of DataDirectory. Try this (line feeds added for readability):
<add name="MovieDBContext"
connectionString="Data Source=(LocalDB)\v11.0;
AttachDbFilename=|DataDirectory|\Movies.mdf;
Integrated Security=True"
providerName="System.Data.SqlClient" />
Apparently, as Ken said, the MoveDBContext was not being referenced.
I removed the entire connectionString from the web.config and everything still functioned correctly.
So, it still begs the question, "How did Visual Studio know to create a database in my SQLExpress instance?" and "Where is that configured at?"

SimpleMembershipInitializer won't initialize

I am struggling with getting a simplemembership scenario working in my EntityFramework / MVC4 / DatabaseFirst project. I've found plenty of examples for working with code first, but nothing for DB first.
The problem I'm encountering is the the InitializeDatabaseConnection is throwing an error ("Unable to find the requested .Net Framework Data Provider. It may not be installed.") The code looks like this :
WebSecurity.InitializeDatabaseConnection("DALEntities", "tblContacts1", "ContactID", "EMail", autoCreateTables: true);
I am not sure what DataProvider is failing. If I try to trace 'into' the InitializeDatabaseConnection call, it immediately throws the error.
What am I missing?
Info:
DALEntities is the name of the connectionString that the rest of EF uses. The following code works just fine....
public ActionResult Test() {
using (var db = new DALEntities()) {
var query = from i in db.TBLINVENTORies
orderby i.ITEMNAME
select i;
var cnt = query.Count();
string str = "Total Inventory: " + cnt;
return Content(str);
}
}
My connection strings section from the web.config:
<connectionStrings>
<add name="DALEntities" connectionString="metadata=res://*/DAL.DAL.csdl|res://*/DAL.DAL.ssdl|res://*/DAL.DAL.msl;provider=System.Data.SqlClient;provider connection string="data source=SOMECOMPUTER;initial catalog=SOMEDB;persist security info=True;user id=SOMEID;password=SOMEPASS;multipleactiveresultsets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
This post seems to be asking the same question (but in context of model-first), but there is no solution yet : Using SimpleMembership with EF model-first
Also, I see that there is an overload for WebSecurity.InitializeDatabaseConnection() with the help text: Initializes the membership system ((blah blah <snip> ProviderName: the name of the ADO.NET data provider. If you want to use Microsoft SQL Server, the WebMatrix.WebData.WebSecurity.InitializeDatabaseConnection(String, String, String, String, Boolean) overload is recommended . I do wish to connect to a MSSQL server...will this be required?
The provider giving you trouble is the one specified in your connection string which is System.Data.EntityClient. I suspect the problem is because your project is database-first whereas the simple membership is using code-first. I do not think you can mix these approaches in a single database. Try putting it back to where the DefaultConnection is used for IntializeDatabaseConnection. There should have been a DefaultConnection in your web.config generated by the MVC4 scaffolding. This connection string usually uses System.Data.SqlClient as the provider.
If you want to keep the simple membership in the database used to store domain information (i.e. DALEntities) then you will need to change your method for using EF on the domain to code-first. If you want to keep your project database-first you need to design your own membership schema in the database and develop custom member and role providers. This is probably the best approach if you are really trying to integrate user information into your domain model.
For simplicity and clarity keep two connection strings pointing to the same database. One for EF (doesn't matter if code first or data first) and one for your WebSecurity stuff. (As Chad and Erik said)
<add name="DataEntities" connectionString="metadata=res://*/Models.DataEntities.csdl|res://*/Models.DataEntities.ssdl|res://*/Models.DataEntities.msl;provider=System.Data.SqlClient;provider connection string='data source=[YOUR_SERVER];initial catalog=[YOUR_DATABASE];integrated security=True;MultipleActiveResultSets=True;App=[YOUR_APP_NAME]'" providerName="System.Data.EntityClient" />
<add name="DataDB" connectionString="data source=[YOUR_SERVER];initial catalog=[YOUR_DATABASE];integrated security=True;MultipleActiveResultSets=True;App=[YOUR_APP_NAME]" providerName="System.Data.SqlClient" />
Note that the providers are different: EF uses System.data.EntityClient and non-EF one uses System.Data.SqlClient.

how to vb.net programmatically specify connection string in dataset.xsd in vs2005

I want to create a dataset.xsd in vs2005, and I am using access database, so I cant know where my client save the application. Hence I used application.startuppath() to get the application folder and appended "Data\db.msd" to the application.startuppath() so i got the target location for the access databse in client machine. Now to create crystal reports I need the dataset.xsd but while creating a new dataset.xsd it was asking the path for the access database, how to programmatically specify the connection string in dataset.xsd so that i can create a connection string. and use that dataset for creating crystal reports.
Thanks in advance
Is it possible that the connection is only used to get the schema of the dataset.
Will you be filling the dataset using a DataReader?
If so i dont think the connection to the dataset is important, it is only to get the schema. You can create the dataset amnually, if it not to mutch effort, and populate it using the appropriate data reader.
You can use AppSettings to store the connection string and retrieve it at runtime. You can then store the connection string information under configuration -> connectionStrings and retrieve it from the application. You could then modify the connection string without having to modify the code.
Your app.settings would look like this (simplified) and replace the ****** with your own connection string:
<configuration>
<connectionStrings>
<add connectionString ="******" name="AccessConnectionString"/>
</connectionStrings>
<appSettings>
</appSettings>
</configuration>
Dim AccessConnectionString As String = System.Configuration.ConfigurationManager.ConnectionStrings.Item("AccessConnectionString").ConnectionString
theConnectionObject.ConnectionString = AccessConnectionString
theDataAdapter.Fill(theDataSet)