Can anyone point me towards a current library that provides basic wrapping of ADO.NET functionality? I'm looking for something along the lines of the old SqlHelper class and am not really interested in using the Data Access Application Block (as it's a bit of overkill for my simple needs). What is everyone using for working with ADO.NET directly these days?
Update:
I should note that I'm already working with an ORM (Nhibernate); I've just run up against a situation that requires raw ADO.NET calls - so no need to suggest using an ORM instead of working with ADO.NET
Dan, this is a class that I have built up over a few years. I use ADO.NET extensivly. It supports simple things like Fill, NonQuery, Scalar, but also getting a schema, transactions, bulk inserts, and more.
DataAdapter (VisualStudio 2010 solution)
Let me know if you need any more help using this (note: I removed some links to other objects to post this for you, so if it's broken, just let me know).
I've written my own little helper library (one .cs file) here:
https://github.com/jhgbrt/yadal/blob/master/Net.Code.ADONet.SingleFile/netstandard/Db.cs
You can find the non-joined version, tests, and readme here:
https://github.com/jhgbrt/yadal
I ended up going with Fluent Ado.net for this; great little library for doing the simple out-of-band ado.net stuff that pops up every now and then.
Hope it helpful:
public static class DbHelper {
public static IDbCommand CreateCommand(this IDbConnection conn, string sql, params object[] args) {
if (!(conn is SqlConnection))
throw new NotSupportedException();
var command = (SqlCommand)conn.CreateCommand();
try {
var paramterNames = new List<string>(args.Length);
for (int i = 0; i < args.Length; i++) {
string name = "#p" + i;
command.Parameters.AddWithValue(name, args[i]);
paramterNames.Add(name);
}
command.CommandText = string.Format(sql, paramterNames.ToArray());
}
catch (Exception) {
if (command != null)
command.Dispose();
throw;
}
return command;
}
}
Related
We have a project configured with Entity Framework v6.1.3 with a database-first approach and TargetFramework="net45". We are using Devexpress OData web API service.
We have been updating entities for years and it is working fine however when it comes to large volume of data our WPF application gets slow at first time.
We believe when we execute query for the first time it gets slow and then we close and reopen same form it becomes faster so we might believe using Entity Framework 6 Power Tools Community Edition could solve our problem regarding of slowness.
https://learn.microsoft.com/en-us/ef/ef6/fundamentals/performance/pre-generated-views
We used this Microsoft link to get help however we didn't get much help on this
We have come so far to generate (Pre-generated mapping views and DataModel.Views.cs) files successfully as per describe in the link however the further process we did not get much help and getting no idea how we can properly use it
var objectContext = ((IObjectContextAdapter) dbContext).ObjectContext;
var mappingCollection = (StorageMappingItemCollection)objectContext.MetadataWorkspace
.GetItemCollection(DataSpace.CSSpace);
if anyone could help on related to this topic it would be much help
public class objectMapping
{
MyDB_Entities context { get; set; }
public objectMapping()
{
context = new ERPDB_Entities();
GenerateViewCache();
}
private void GenerateViewCache()
{
var objectContext = ((IObjectContextAdapter)context).ObjectContext;
var mappingCollection = (StorageMappingItemCollection)objectContext.MetadataWorkspace.GetItemCollection(DataSpace.CSSpace);
var mappingHashValue = mappingCollection.ComputeMappingHashValue();
var edmSchemaError = new List<EdmSchemaError>();
var views = mappingCollection.GenerateViews(edmSchemaError);
}
}
I created above class and call in Application_Start to Generate ViewCache
protected void Application_Start()
{
new objectMapping();
}
does any one knows that i implemented this in right way?
Everytime when I open the database connection, the very first interaction with the database takes a lot of time. From the second interaction on the "speed" is much higher. I think it's an optimization of the Entity Framework and it looks like something good. But it's still the same even if I'm working with more than one database. The very first interaction with the first database is slow. But the first interaction with the second database is fast. Why isn't the first interaction with another database slow too? Another problem is that the Initializer only works with the first database. (I think this problem is caused by this optimization)
Related to this question (asked by myself):
Entity Framework 6 SetInitializer DbContext does not work for SQL Server Express and SQL Server Compact
Does anyone know how this optimization works and how to disable it? I don't actually work with the databases I compare them, for this case it would be nice to disable any optimization.
I'm wrote an answer as comment is not appropriate.
For the speed Yuliam pointed you to the good link.
For the rest, I'm not sure to understand your problem.
For me the following code (pertinent extract)
class Program {
static void Main(string[] args) {
Database.SetInitializer<TestEFContext>(new DropCreateDatabaseAlways<TestEFContext>());
String cs = #"Data Source=ALIASTVALK;Initial Catalog=TestEF;Integrated Security=True; MultipleActiveResultSets=True";
using (TestEFContext ctx = new TestEFContext(cs)) {
Console.WriteLine(ctx.Orders.Count());
}
cs = #"Data Source=ALIASTVALK;Initial Catalog=TestEF2;Integrated Security=True; MultipleActiveResultSets=True";
using (TestEFContext ctx = new TestEFContext(cs)) {
Console.WriteLine(ctx.Orders.Count());
}
}
}
well create and recreate databases TestEF and TestEF2.
Did I misunderstand your question ?
===== Response to comment:
In this case both databases are always (re)created:
first run: creation
use SSMS to populate some data in some tables
second run
check tables content with SSMS : tables are empty <=> databases were recreated at second run.
Only trivial difference with your case: I use EF 6;
What is the best way to identify the source of a (very) inefficient Entity Framework query when I only have the captured SQL available?
Assuming you're using some kind of logging framework, you could do something like this in your DbContext constructor:
this.Database.Log = sql =>
{
if (sql.Contains(/* part of the offending query */))
{
Logger.Log($"Found it! {Environment.StackTrace}")
}
}
I'm using Entity Framework 5 with Code First. I've written a custom IDbConnectionFactory which I want to use for all the connections made by my DbContext class, so early on in the application's lifecycle, before any database work is done, I call
Database.DefaultConnectionFactory = new MyConnectionFactory();
However, MyConnectionFactory.CreateConnection is never called, which suggests to me that EF's changed it back - but the debugger shows that it's still a MyConnectionFactory after several queries have run. For some reason, it's just not using it.
My DbContext is initialised by passing the name of a connection string from the app.config file, and those connection strings do specify an explicit provider (as indeed they have to) so I'm wondering if that's causing a per-connection override of the connection factory based on the connection string. Does this happen and can I stop it without registering a completely new provider (although maybe that's not too hard to do?).
Whatever I see online about this (much obscured by the defaultConnectionFactory tag in various app.config examples) suggests you can just change it to an IDbConnectionFactory instance of your choice and it'll work, but mine isn't behaving.
The purpose of this is to allow me to run a particular set of SQL statements whenever a new connection is opened, so the second part of this question would be does anybody know a better way to do this?
I know it is not ideal but this worked for me:
public class DBBase : DbContext
{
public DBBase(string nameOrConnectionString)
: base(Database.DefaultConnectionFactory.CreateConnection(nameOrConnectionString), true)
{
}
// ...
}
You need to get the connection that you built for each call that you are wanting to use. For example using the following code.
private static void UsingCustomConnection()
{
using (var conn = Database.DefaultConnectionFactory.CreateConnection("YourDbName"))
{
using (var context = new YourContext(conn))
{
context.Destinations.Add(new Destination {Name = "Colorado"});
context.SaveChanges();
}
}
}
You will need to setup this in YourContext
public YourContext(DbConnection connection)
: base(connection, contextOwnsConnection: false)
{
}
I have entities that I am getting via Entity Framework. I'm using Code-First so they're POCOs. When I try to XML Serialize them using XmlSerializer, I get the following error:
The type
System.Data.Entity.DynamicProxies.Song_C59F4614EED1B7373D79AAB4E7263036C9CF6543274A9D62A9D8494FB01F2127
was not expected. Use the XmlInclude
or SoapInclude attribute to specify
types that are not known statically.
Anybody got any ideas on how to get around this (short of creating a whole new object)?
Just saying POCO doesn't really help (especially in this case since it looks like you're using proxies). Proxies come in handy in a lot of cases but make things like serialization more difficult since the actual object being serialized is not really your object but an instance of a proxy.
This blog post should give you your answer.
http://blogs.msdn.com/b/adonet/archive/2010/01/05/poco-proxies-part-2-serializing-poco-proxies.aspx
Sorry, I know I'm coming at this a bit late (a couple YEARS late), but if you don't need the proxy objects for lazy loading, you can do this:
Configuration.ProxyCreationEnabled = false;
in your Context. Worked like a charm for me. Shiv Kumar actually gives better insight into why, but this at least will get you back to work (again, assuming you don't need the proxies).
Another way that works independent of the database configuration is by doing a deep clone of your object(s).
I use Automapper (https://www.nuget.org/packages/AutoMapper/) for this in my code-first EF project. Here is some sample code that exports a list of an EF objects called 'IonPair':
public bool ExportIonPairs(List<IonPair> ionPairList, string filePath)
{
Mapper.CreateMap<IonPair, IonPair>(); //creates the mapping
var clonedList = Mapper.Map<List<IonPair>>(ionPairList); // deep-clones the list. EF's 'DynamicProxies' are automatically ignored.
var ionPairCollection = new IonPairCollection { IonPairs = clonedList };
var serializer = new XmlSerializer(typeof(IonPairCollection));
try
{
using (var writer = new StreamWriter(filePath))
{
serializer.Serialize(writer, ionPairCollection);
}
}
catch (Exception exception)
{
string message = string.Format(
"Trying to export to the file '{0}' but there was an error. Details: {1}",
filePath, exception.Message);
throw new IOException(message, exception);
}
return true;
}