Could not get reference of System.Data in Asp.Net 5 - ado.net

I am building an web application using ASP.NET5 MVC6 and Classic Ado.Net.
But we could not take reference of System.Data. Only two packages are available in nuget packages
System.Data.Common and System.Data.SqlClient.
In addition to that, whenever I add System.Data.Common from nuget, my project is not compiling.
I need DataSet and Datatable in the controller's action method.
How could we get this in Asp.Net 5 ?
I have got answer from this question.

The current version of System.Data.Common and System.Data.SqlClient. are conflicting with other asp.net-5 libraries.
You can use EF7 and use the connection object from the DBConect.Database
var conn = DbContext.Database.GetDbConnection();
var cmd=conn.CreateCommand();
cmd.CommandText = "SELECT 1";
conn.Open();
var dr = cmd.ExecuteReader();
conn.Close();

Related

Entity Framework new object vs DbSet.Create

We are migrating a web application from EF 4.0 to EF 6. The entities were earlier based on ObjectContext, but now we are looking at DBContext. The code heavily depends on lazy loading. Entities are added using following syntax:
var user = new EntityModel.User();
user.DepratmentId=25;
context.Users.Add(user);
context.SaveChanges();
var name = user.Department.Name;
the original code would easily assign department name into variable name. After Entity framework upgrade to EF6 with DBContext, user.Department is null. I understand that when we are using DBContext, Lazy Loading works only with Proxies. It would work fine if the code was changed to following:
var user = context.Users.Create();
user.DepratmentId=25;
context.Users.Add(user);
context.SaveChanges();
var name = user.Department.Name;
Problem at my hand is that we can not make this change in the whole code base. Given the large volume of code, this is practically impossible. Does someone have a solution to this?
Provided your entities are easily identifiable such as all being pulled from the namespace "EntityModel" then VS's Find & Replace can help with the transition. Ultimately you're going to have to eat the cost of that technical debt. Re-factoring isn't free, but the benefit from making improvements (beyond just upgrading a dependency version) should outweigh that cost.
Using Find & Replace:
Find: = new EntityModel.(?<class>.*)\(\)
Replace: = context.${class}s.Create()
This will find instances like:
var user = new EntityModel.User();
and replace it with var user = context.Users.Create();
a test with:
var user = new EntityModel.User();
var test = new EntityModel.Test();
var fudge = new EntityModel.Fudge();
resulted in:
var user = context.Users.Create();
var test = context.Tests.Create();
var fudge = context.Fudges.Create();
Now this will extract the class name and pluralize it with an 's' which likely won't match 100% of the entity DBSet names, but those are easily found and corrected. The expressions can be tuned to suit differences through the application and I would recommend performing the operation on a file by file, or at most project by project basis.
An caveat is to make sure you're running with source control so that any bad attempts at a replace can be rolled back safely.

What happened to AddExportedObject?

we have source code from a long time ago .NET 2.0 which looks like:
DirectoryCatalog catalog =
new DirectoryCatalog(Path.Combine(Path.Combine(applicationDirectory, providersDirectory), providerSubdirectory));
CompositionContainer container = new CompositionContainer(catalog);
CompositionBatch batch = new CompositionBatch();
batch.AddPart(this);
batch.AddExportedObject(container);
container.Compose(batch);
This has worked fine for years, but after upgrading visual studio and the framework to .NET 4.0, the call to AddExportedObject no longer seems to be supported by MEF.
As you can read here,
While CompositionBatch itself does not contain an AddExportedObject<T>
there is an extension method for it. The extension methods are in the
root System.ComponentModel.Composition namespace so ensure you are
including that in your file's using statements. If you cannot get the
extension method to work you can always call the method directly on
AttributedModelServices.

Phonegap and Nova Data Framework -

I am learning PhoneGap for an app project and need to use the database for certain aspects, I am trying out the Nova Data framework,
https://cordova.codeplex.com/wikipage?title=How%20to%20use%20nova.data
I am trying to use my code to put together a test entity, but I am getting a db error telling me there is a missing table. The documentation does not specify that the database should be created beforehand, but I am starting to think that may be the case. Has anyone out there used the Nova framework in a project? I just need a little guidance.
Here is my code I am using to kick off the DB Context:
var DataContext = function () {
nova.data.DbContext.call(this, "HealthDb", "1.0", "Health DB", 1000000);
this.Temperatures = new nova.data.Repository(this, Temperature, "Temperatures");
};
DataContext.prototype = new nova.data.DbContext();
DataContext.constructor = DataContext;
And my entity (Temperature) :
var Temperature = function () {
nova.data.Entity.call(this);
this.Value = 101;
};
Temperature.prototype = new nova.data.Entity();
Temperature.constructor = Temperature;
It is creating an empty database with the proper name, just no tables! I am grateful for any assistance!
Thanks for using our library. I have made the html5 sqlite as a standalone library. Please get it from github.
A live demo link is also available there. And the documentation is more complete. The lib itself has also been updated and a few bugs fixed.
Thanks,
Leo
Turns out I was trying to start up the dbcontext before I defined my entity classes....
Changed the order of my js files and it works.

db4o creating event not fire on db4o 8

i am using db4o 8 with c# 3.5, The TA and TP is enabled on all of my domain model classes.
the problem is i have my own ID Generator attached to creating event with following code:
IEventRegistry eventRegistry = EventRegistryFactory.ForObjectContainer(Container);
eventRegistry.Creating += new EventHandler(eventRegistry_Creating);
i have a USER class containing a list of ORDER.
problem is if i update the USER class, creating event does not fire for new added ORDER objects in USER.ORDERS.
before version 8 i used v7.4 and it worked fine, but today i upgraded it to v8 to gain some performance benefits but this problem occurred.
would you please help me to fix this problem ?
I tried to reproduce the issue and it worked for me fine. Are you sure that the added order is actually stored? What kind of collection are you using? The db4o activatable collections or the regular CLR collections? And which version did you use?
Here my little test-case which worked:
var eventRegistry = EventRegistryFactory.ForObjectContainer(container);
var expectFireCreated = false;
eventRegistry.Created += (sender, args) =>
{
expectFireCreated = true;
};
var costumer = (from Constumer c in container
select c).First();
costumer.Orders.Add(new Order("55"));
container.Commit();
Assert.IsTrue(expectFireCreated);

How to use ADO.net Entity Framework with an existing SqlConnection?

I have an existing asp.net website that uses an SqlConnection.
I have added the ADO.net Entity Framework.
I have successfully connected to the database and created the .edmx file.
I am able to connect through the Entity Framework with the connectionstring that is automatically generated.
I want to use the existing SqlConnection object that I use throughout the site for the Entity Framework connection.
I do not want to have to use a second database connection for the one page that is going to use the ADO.net Entity Framework and I don’t want to change the entire site to use the new Entity Framework connection string.
Thanks for any help you can provide.
That forum post has the answer:
MetadataWorkspace workspace = new MetadataWorkspace(
new string[] { "res://*/" },
new Assembly[] { Assembly.GetExecutingAssembly() });
using (SqlConnection sqlConnection = new SqlConnection(connectionString))
using (EntityConnection entityConnection = new EntityConnection(workspace, sqlConnection))
using (NorthwindEntities context = new NorthwindEntities(entityConnection))
{
foreach (var product in context.Products)
{
Console.WriteLine(product.ProductName);
}
}
"res://*/" is the part of your EF connection string that describes the location of your xml mapping files - in this case embedded resources in the current assembly.
You can do this by using the constructor of your generated ObjectContext that accepts an EntityConnection. When you create the EntityConnection you pass in your SqlConnection.
Andrew Peters,
Thank you for your answer.
I have been going around and around with the System.Data.EntityClient.EntityConnection.
It’s right there at my finger tips but I cannot seem to get the MetadataWorkspace parameter to work.
This is the closest example I have found (the post marked Answer):
http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/dd7b1c41-e428-4e29-ab83-448d3f529ba4/
Thanks for any help.