I have an asp.net web forms application it uses EF for all database activities. When page loads I have to fetch lot of data from different tables. I have a DataAccessor class where I am having a member variable of Entity Framework DbContext (MyDBEntities). See class definition below.
public class DataAccessor
{
public static DataAccessor Instance = new DataAccessor();
private MyDBEntities dbEntities = new MyDBEntities();
private DataAccessor()
{
}
public FetchTable_1_Data()
{
return dbEntities.Table1.Where( x => x.Id = something).List();
}
public FetchTable_2_Data()
{
return dbEntities.Table2.Where( x => x.Id = something).List();
}
public FetchTable_n_Data()
{
return dbEntities.TableN.Where( x => x.Id = something).List();
}
}
Using data accessor as below in page load
Page_Load()
{
Repeater1.DataSource = DataAccessor.Instance.FetchTable_1_Data();
Repeater1.DataBind();
Repeater2.DataSource = DataAccessor.Instance.FetchTable_2_Data();
Repeater2.DataBind();
}
My Questions are,
When DB connection is getting open in my case
When DB connection getting closed?
Do I need to use using(MyDBEntities dbEntities = new MyDBEntities()) instead of using member variable
If i would need to use as question #3, do I need to open connection using "using" statement in each fetch methods?
My database connection is broken sometimes and system performance is getting degrade, i suspect my usage of EF. Can someone advice me how to use EF?
Some more questions,
How connection pooling is working with EF?
connectionString="metadata=res:///ReviewsDb.csdl|res:///ReviewsDb.ssdl|res://*/MyDb.msl;provider=System.Data.SqlClient;provider connection string="data source=SERVER;initial catalog=MyDB;persist security info=True;user id=sa;password=mypwd;multipleactiveresultsets=True;application name=EntityFramework"" providerName="System.Data.EntityClient" />
I am using above connection string, do I have connection pool with above connection string?
How can I configure connection in EF
Appreciate any help on these questions.
thank you friends
You dataaccessor is a static member.
Stay away from using static since it may survive between page accesses. It is not hard to imagine what damage that could cause.
I have got database errors from the previous page access when I was doing that. I had to scratch my head a lot.
Also use == when comparing instead of =.
You donĀ“t have to use using.
I would actually advise you to have a data accessor that your Page_Load will create with something like var accessor = new DataAccesssor(); instead of using your approach with a private constructor.
Now it will be clear for you which lifetime your MyDbEntities instance will have. It will be scoped to your Page_Load function.
Related
I'm fairly new to C# programming but am competent in SQL. I'm trying to write my first application (a data monitoring system) which saves SQL statements and thresholds from user input then on a squeal, runs the statements and checks to see if they have breached their respective threshold, this then alerts the user on screen and also sends an email/text if applicable.
Here I have some code that grabs data from 2 tables (this is ASP.NET Core MVC so they are models):
Widget - Which is where the check information is kept (we only care about the statement and connection string id.
ConnectionStrings - Which is where each connection string is stored.
I'm trying to get it to pull back a list of the checks, then their corresponding connection string, pass them into SqlConnection or a Dapper method and log the results to a table where they can be passed back to the user.
The bit I'm stuck at is that GetConnectionStringId and Query, both of which throw the error mentioned in the title. Obviously I'm trying to do a foreach in the list, using .FistOrDefault is not the answer I'm looking for here unless anyone can properly explain why it should be. It turns my IEnumerable error into a bool error and should be needless to say none of these values are booleans.
Is there a simple way around this or am I going about this the completely wrong way?
Don't be shy with a response I'm quite happy to be told I've got no clue what I'm doing, thanks in advance!
Also if more information is needed please let me know!
using Dapper;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System.Data;
using SystemBoard.Data;
namespace SystemBoard.Controllers
{
public class WidgetCheckController : Controller
{
private readonly ApplicationDbContext _context;
public WidgetCheckController(ApplicationDbContext context)
{
_context = context;
}
// * Testing with list idea
public async Task<List<IActionResult>> GetCheckResult()
{
var checks = await _context.Widget
.Where(x => x.DateDeleted == null)
.ToListAsync();
foreach (var WidgetId in checks)
{
var GetConnectionStringId = checks.Select(x => x.ConStringId);
var ActualConnectionString = _context.ConnectionStrings.First(x => x.ConnectionStringId = GetConnectionStringId);
// I don't know if it's better to write it like this? I get the same error under 'checks.Select(x => x.ConStringId)'
// var ActualConnectionString = _context.ConnectionStrings.First(x => x.ConnectionStringId = checks.Select(x => x.ConStringId));
var Query = checks.Select(x => x.SQLStatement);
using (IDbConnection connection = ActualConnectionString)
{
connection.Query(Query);
}
}
}
}
}
I am trying to execute below query
using (var dbcontext = new EVEntities())
{
var data_header = dbcontext.Cl.Where(x => x.PKey ==
header_key).FirstOrDefault();
if (data_header != null)
{
data_header.EstimatedCost = Math.Round(estimated_cost,2);
data_header.ClaimedCost = Math.Round(claimed_cost,2);
dbcontext.Entry<Cl>(data_header).State = System.Data.Entity.EntityState.Modified;
dbcontext.SaveChanges();
Writelog("Updated");
}
}
Here Writelog write in a text file and it is working always. But the field in Cl is not getting updated. In between the data is getting updated also.
Connection String
<connectionStrings><add name="EVEntities" connectionString="metadata=res://*/xxx_Entity_Model.csdl|res://*/xxx_Entity_Model.ssdl|res://*/xxx_Entity_Model.msl; provider=System.Data.SqlClient;provider connection string="data source=xxxxxx;initial catalog=xxxxx;persist security info=True;user id=xx;password=xxxxx;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient"/></connectionStrings>
Edit1
tried raw update also
dbcontext.Database.ExecuteSqlCommand(#"UPDATE dbo.Claims
SET EstimatedCost = #e_cost, ClaimedCost = #c_cost WHERE Pkey =
#p_key",
new SqlParameter("e_cost", Math.Round(estimated_cost, 2)),
new SqlParameter("c_cost", Math.Round(claimed_cost, 2)),
new SqlParameter("p_key", claim_header_key));
same outcome. It get updated in between. No error.
You didn't give us enough information to give you the solution. Therefore I give you a method to debug the problem.
Does the DbContext think that anything must be saved?
What SQL is sent to the database?
When you call SaveChanges, DbContext checks its ChangeTracker to see if anything must be updated. Consider to write some Debug code to detect whether there are changes.
Insert just before SaveChanges:
bool changesDetected = dbContext.ChangeTracker.HasChanges;
It might be that you need to call DetectChanges() first. I'm not sure.
If there are Changes, check if the item that you think that should be updated is changed:
IEnumerable <DbEntityEntry<Cl>> entries = dbContext.ChangeTracker.Entries<Cl>;
// We're expecting exactly one entry:
DbEntityEntry<Cl> myEntry = entries.SingleOrDefault();
Assert(myEntry != null);
If null, try to find out why it is not tracked. Was it tracked after you fetched it, before you changed it? Do you have somewhere tracking switched off? Write some other debug code where you fetch some other data. Is that tracked?
If not null, then apparently your Cl is tracked. It ought to be changed:
Assert(myEntry.State == EntityState.Modified);
If not modified, fetch the original values and the current values:
DbPropertyValues originalValues = myEntry.OriginalValues;
DbPropertyValues currentValues = myEntry.currentValues;
In your debugger, check them, or write some debug code to compare the original value with the current values. Are the changed values correct?
I'm not sure if entity framework will try to update objects that are unmodified and of which the original values are not equal to the current values. We'll find out to see what SQL is created when you do the SaveChanges.
It would be nice if your database can log all communications.
You can also log what entity framework sends to your database. For this, use property DbContext.Database.Log. For example:
dbContext.Database.Log = Console.Write;
dbContext.SaveChanges();
If you can't write to Console, write a method:
private List<string> SqlCommands {get;} = new List<string>();
void LogSqlCommands(string sqlCommand)
{
this.SqlCommands.Add(sqlCommand);
}
And in your method that following debug code:
using (var dbcontext = new EVEntities())
{
this.SqlCommands.Clear();
dbContext.Database.Log = this.LogSqlCommands;
var data_header = ... etc
dbContext.SaveChanges();
}
Put a breakpoint after SaveChanges and check the generated SQL.
Hope these debugging tips help you to find the cause of your problem
Here is what i have so far :
<add name="gymEntities1" connectionString="metadata=res://*/DateModel.csdl|res://*/DateModel.ssdl|res://*/DateModel.msl;provider=System.Data.SqlClient;provider connection string="data source=.;initial catalog=gym;user id=sa;password=xxxx;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
it works on my LocalHost Database, and I can load my Data from it.
however, I have a server, installed sqlserver with my database on that, basicaly when i change my sqlcommands connecting string this work but in some part of my program I used entity framework and have no idea how to change it connecting string, with some posts in stackoverflow I change that to
<add name="gymEntities2" connectionString="metadata=res://*/DataModel.csdl|res://*/DataModel.ssdl|res://*/DataModel.msl;provider=System.Data.SqlClient;provider connection string="data source=tcp:46.105.124.144;initial catalog = gym ;User ID=sa;Password=xxxx"" providerName="System.Data.EntityClient" />
but it still read datas from my localhost and not connect to my server.
I don't know how when i change this connecting string to my server it still read data from my localhost database.
what is the best way to change connecting string from App.Config?
First Possible Issue:
It is less likely since other people suggested to you.But, it is possible that you are missing a connection string in one of your web.config or app.config.
It is a good habit to copy your string to every project. Example. I have 3 different projects in my solution (Library, WCF, WPF). I copied the following connection string to each project (One sample for Local SQL Server and another for Azure):
<connectionStrings>
<add name="LocalSQLServerSample.CodeREDEntities" connectionString="metadata=res://*/CodeRED.csdl|res://*/CodeRED.ssdl|res://*/CodeRED.msl;provider=System.Data.SqlClient;provider connection string="data source=MachineName\ServerName;initial catalog=CodeRED;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
<add name="AzureSQLServerSample.CodeREDEntities" connectionString="metadata=res://*/CodeRED.csdl|res://*/CodeRED.ssdl|res://*/CodeRED.msl;provider=System.Data.SqlClient;provider connection string='data source=azureservername.database.windows.net;initial catalog="CodeRED";persist security info=True;user id=CodeRED;password=R%Chd$g*VHs28eEr;MultipleActiveResultSets=True;App=EntityFramework'" providerName="System.Data.EntityClient" />
</connectionStrings>
Second possible issue:
You have mentioned that you are using entity framework. Are you using ObjectContext to access it? If yes, I have the method below to call it every time I want to access any database:
From sample above: name="LocalSQLServerSample.CodeREDEntities"
_containerName is CodeREDEntities (The same for all my connections).
environment is to determine which database you are connecting to. For example, in the above connection sample, I have LocalSQLServerSample and AzureSQLServerSample and I usually have something like PRODUCTION, DEVELOPMENT, TESTING....
public static ObjectContext getObjectContext(string environment, bool isReadOnly)
{
environment = environment == null ? "" : environment.Trim();
environment = environment.Length == 0 ? "" : (environment + ".");
ObjectContext objectContext = new ObjectContext(
ConfigurationManager.ConnectionStrings[environment + _containerName].ToString());
objectContext.DefaultContainerName = _containerName;
objectContext.CommandTimeout = 0;
objectContext.ContextOptions.ProxyCreationEnabled = !isReadOnly;
return objectContext;
}
Sample of how to use it:
Common is a common class that I use to store shared information such as getting common error format used for Common.getInnerExceptionMessage.
Also, you don't have to always pass environment, you can store it as a constant to be able to call it such as (I always pass it to be able to mix connection when I need to for specific calls): You can modify connection from anywhere by changing _selectedEnvironment if you do not wish to pass it everywhere.
public const string _ENVIRONMENT_DEVELOPMENT = "LocalSQLServerSample";
public const string _ENVIRONMENT_PRODUCTION = "AzureSQLServerSample";
public static string _selectedEnvironment = _ENVIRONMENT_PRODUCTION;
Sample of getting item based on id:
Note: User is a class generated by entity framework from database.
public UsersDataGrid GetItem(string environment, long id)
{
ObjectContext objectContext = Common.getObjectContext(environment, false);
try
{
var item = objectContext.CreateObjectSet<User>()
.Where(W => W.ID == id)
.Select(S => new UsersDataGrid()
{
Active = S.Active,
ID = S.ID,
Unique_ID = S.Unique_ID,
First_Name = S.First_Name.ToUpper(),
Last_Name = S.Last_Name.ToUpper(),
Email = S.Email,
School = S.School.Title.ToUpper(),
Gender = S.Gender.Title.ToUpper(),
TShirt_Size = S.TShirt_Size.Title.ToUpper(),
GUID = S.GUID + "",
Note = S.Note,
Machine_User = S.Machine_User,
Machine_Name = S.Machine_Name,
Created_On = S.Created_On,
Last_Updated_On = S.Updated_On
}).FirstOrDefault();
return item;
}
catch (Exception exception)
{
return new UsersDataGrid()
{
Note = ("Service Error: " +
Common.getInnerExceptionMessage(exception))
};
}
}
2nd Sample: Updating a user:
Note: Common.CopyValuesFromSourceToDestinationForUpdate is only a generalized method copying items from item object to entityItem, instead you can copy values normally such as entityItem.ID = item.ID and so on...
public Result Update(string environment, User item)
{
ObjectContext objectContext = WCF_Service_Library.Classes.Common.getObjectContext(environment, false);
try
{
var entityItem = objectContext.CreateObjectSet<User>()
.AsEnumerable().Where(Item => Item.ID == item.ID).ToList().FirstOrDefault();
if (entityItem == null)
return new Result("Item does NOT exist in the database!");
entityItem = Common.CopyValuesFromSourceToDestinationForUpdate(item, entityItem) as User;
objectContext.SaveChanges();
return new Result(entityItem.ID);
}
catch (Exception exception)
{
return new Result("Service Error: " + Common.getInnerExceptionMessage(exception));
}
}
Third issue (it does not look like it, but you may encounter it):
If you publish your app and ONLY sign your WPF project, you will not get error during publishing, but you may not be able to connect to the database. You must sign all your projects in your solution.
Hopefully this help you with your problem
Check the WebConfig of your startup project. Entity framework reads ConnnectionString from AppConfig when you run Update Model From Db operation .
But in runtime it reads ConnnectionString from WebConfig in your Startup project
I hope it is use for you
Add this for App.Config files
<connectionStrings>
<add name="Dbconnection"
connectionString="Server=localhost; Database=OnlineShopping;
Integrated Security=True"; providerName="System.Data.SqlClient" />
</connectionStrings>
This connection string must be work:
<add name="Name"
connectionString="metadata=<Conceptual Model>|<Store Model>|<Mapping Model>;
provider=<Underlying Connection Provider>;
provider connection string="<Underlying ConnectionString>""
providerName="System.Data.EntityClient"/>
If you have any problems with writing connection string, you can use following code on the page.
I have an application that I want to be able to configure the connection string for my LINQ to SQL. I've tried so many different ways but cannot seem to get it working. I want to do this dynamically in the code when the app is run, the reason for this is that the user can change the connection settings.
If I delete the connectionString out of app.config the application still works OK (communicating) which makes me wonder where I should be changing the connection string?
I think that the best way to do it is a combination of Albin's and Rup's answers. Have a value in the config file, and then read it at run time and feed it to the context constructor, something like this:
WEB.CONFIG:
<appSettings>
<add key="ConString" Value="The connection string" />
CODE:
//read value from config
var DBConnString = System.Configuration.ConfigurationManager.AppSettings("ConString");
//open connection
var dataContext= new MyDataContext(sDBConnString)
this way you can change the connection string even at runtime and it will work and change on the running program.
You can pass an override connection string into the DataContext constructor:
var db = new MyDataContext("Data Source=Something Else;")
The DBML class (YourDataContext) has an overloaded constructor which takes ConnectionString, so try instantiating that instead of the default one.Get the connection string from app.config and use that to create the instance.
YourDataContext context = new YourDataContext (ConfigurationManager.ConnectionStrings["ConnStringInAppConfig"].ConnectionString)
You should change it in app.config. The reason it works without is that the LINQ2SQL designer creates a fallback to the connection string used when designing the DBML. If you define a connection string in app.config that is used instead.
By Default your constructor look like this
public dbDataContext() :
base(global::invdb.Properties.Settings.Default.Enventory_4_0ConnectionString, mappingSource)
{
OnCreated();
}
You can change return value Instead of
//Original
public string Enventory_4_0ConnectionString {
get {
return ((string)(this["Enventory_4_0ConnectionString"]));
}
}
this
//Modified code
public string Enventory_4_0ConnectionString {
get {
return (System.Configuration.ConfigurationManager.ConnectionStrings["Enventory_4_0ConnectionString"].ConnectionString);
}
}
Inside your dbml file designer.cs add this dynamic call to base class constructor. It will work for local, dev and prod automatically pulling from current web.config without need to pass connection every time;
public HallLockerDataContext() :
base(ConfigurationManager.ConnectionStrings["MYDB1"].ConnectionString, mappingSource)
{
OnCreated();
}
Usage:
using (var db = new HallLockerDataContext())
{
}
I've got a SqlServer project with a very simple test for a Table-Valued-Function:-
[SqlFunction(TableDefinition = "forename nvarchar(50)", FillRowMethodName = "TestFillRow", DataAccess = DataAccessKind.Read)]
public static IEnumerable TestConn(int ID)
{
using (SqlConnection con = new SqlConnection("context connection=true"))
{
//con.Open();
yield return "Anthony";
}
}
public static void TestFillRow(object obj, out string forename)
{
forename = (string)obj;
}
Note the Open on the connection is currently commented out. Once deployed I can execute like this in SQL:-
SELECT * FROM [dbo].[TestConn](1)
All works fine.
Now I uncomment the con.open() and it fails with:-
Data access is not allowed in this
context. Either the context is a
function or method not marked with
DataAccessKind.Read or
SystemDataAccessKind.Read, is a
callback to obtain data from FillRow
method of a Table Valued Function, or
is a UDT validation method.
I don't see what the problem is, the TestConn function has got DataAccessKind.Read.
Anyone know of any other reasons for getting this error?
The problem is the following:
SQLCLR does not allow any data access inside TestFillRow
Even though it "looks" like your TestFillRow doesnt access data, the way the compiler translates code with "yield" statements is by actually deferring it's execution until the first .MoveNext() call to the iterator. Therefore the following statement:
using (SqlConnection con = new SqlConnection("context connection=true"))
gets executed inside TestFillRow... which is illegal.
Do not use yield return; instead load the whole result to a List<> and return the list at the end of the UD Function.
"SQLCLR does not allow any data access inside TestFillRow" is a mistake.
If you don't use context connection = true connetion string you can access data inside FillRow method.