Reputation: 11
I'm new to C# and asp.net programming and currently use MsSQL DataBases linked to my project with a DBML, using Linq to SQL, with my connection string in my web.config and all my database functions are stored in class managers. Examples below.
Connection string in web.config:
<add name="ConnectionString" connectionString="Data Source=server;Initial Catalog=database;Persist Security Info=True;User ID=UserId;Password=password" providerName="System.Data.SqlClient" />
Example Class Manager:
using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Linq;
using System.Web;
public class DataManager
{
private DataContext dc;
public DataManager()
{
dc = new DataContext();
}
public News GetNews(int newsId)
{
return dc.News.Where(x => x.NewsId == newsId).Where(x => x.Deleted == false).FirstOrDefault();
}
public News News_Save(News n)
{
if (n.NewsId == 0)
dc.News.InsertOnSubmit(n);
dc.SubmitChanges();
return n;
}
}
What I would like to know is how I can have a similar setup to this but to access a MySQL database. All the examples I have seen require creating a connection string within the page each time you want to use a function which seems wasteful and unnecessary to me. Is there anyway I can use my current structure but access a MySQL database with or without a DBML?
Upvotes: 1
Views: 2466
Reputation: 4019
AFAIK, there is no up-to-date Linq-to-SQL provider for MySQL. But there is a MySQL provider for Entity Framework: Connector/Net. Entity Framework also supports LINQ, so your code would be pretty much the same.
You can use the built-in tools in Visual Studio (Project > Add New Item ... > ADO.NET Entity Data Model) to create your entities from an existing database and connection info will be stored in a .config file (I guess that would be web.config for a web/asp.net project).
Upvotes: 1
Reputation: 2151
Microsoft doesn't have a library to access mysql through linq but there are 3rd party providers. See this post. That should mean you can keep your current manager class as-is (or maybe minor modifications to accommodate the 3rd party linq-to-mysql library).
Upvotes: 0