All examples of Using PetaPoco within Umbraco I have found reference the main Umbraco database using (or similar):
var db = applicationContext.DatabaseContext.Database;
I wish to connect to an addition database but I am having trouble.
I have created a connection string within my web.config file with the name of productDbDSN.
I have created the following controller within Visual Studio (based on the documentation on the toptensoftware website) but have the red line under PetaPoco:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Umbraco.Core.Persistence;
namespace Products.Controllers
{
public class ProductsController : Controller
{
public ActionResult Index()
{
var dataContext = new PetaPoco.Database("productsDbDSN");
var products = from p in dataContext
select p;
return View(products.Take(20));
}
}
}
I thought by using umbraco.Core.Persistence I am accessing PetaPoco? Can anybody help?
Looks like it's a typo, the class Database is in the Umbraco.Core.Persistence namespace.
using Umbraco.Core.Persistence; ... var dataContext = new Database("productsDbSDN"); var products = dataContext.Query<Product>("select * from products");
thinking about it that typo makes more sense and is closer to my original code.
However now I am getting an error that (the 2nd) dataContext reference is a variable but used as a method and that the Query<Product> is namespace but used as a type.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Umbraco.Core.Persistence;
namespace Employee.Products
{
[TableName("Products")]
[PrimaryKey("ID")]
public class Employee
{
[Column("ProductID")]
public Int ID { get; set; }
[Column("Product")]
public string Product{ get; set; }
}
}
Petapoco connection to non-umbraco database
All examples of Using PetaPoco within Umbraco I have found reference the main Umbraco database using (or similar):
I wish to connect to an addition database but I am having trouble.
I have created a connection string within my web.config file with the name of productDbDSN. I have created the following controller within Visual Studio (based on the documentation on the toptensoftware website) but have the red line under PetaPoco:
I thought by using umbraco.Core.Persistence I am accessing PetaPoco? Can anybody help?
Hi David
I think all you need is this:
That will get you hooked up. Not sure about the rest though. I personally like to see my SQL:
var products = dataContext ().Query<Product>("select * from products");
Cheers,
Theo
Thanks Theo, I tried that but Visual Studio threw up an error for 'newDatabase' which was:
The name 'newDatabase' does not esixt in the current context
Hi David,
Looks like it's a typo, the class Database is in the Umbraco.Core.Persistence namespace.
HTH,
Hendy
Thanks Hendy,
thinking about it that typo makes more sense and is closer to my original code. However now I am getting an error that (the 2nd) dataContext reference is a variable but used as a method and that the Query<Product> is namespace but used as a type.
Any suggestions?
Hi David,
Do you have a class called Product ? or another model / class to represent a single row in the database table you'd like to query ?
Yes I have the following model called Products
Hi David,
Does the following work ?
is working on a reply...