Reputation: 12706
I have .NET MVC 3 project where I use sql membership provider and entity framework.
I've created new database on my SQL server and created all objects for membership provider using aspnet_regsql.exe
, I've changed my connection string and it works fine.
Now I want to use entity framework for db interaction. I'm using Code-First so I created simple entity class and I can save data to db and retreive it.
The problem is that the place where entity framework keeps it's data is a mistery. I can not find any database in my project and there are no new tables in my SQL database.
If I add second connection string to web.config (the first one is for membership provider) my entity framework stops working (I assume it should create new tables in my DB) but it does not do it.
My entity class looks like:
public class MyEntities : DbContext {
public DbSet<Business> Businesses { get; set; }
}
And I'm getting an exception Invalid object name 'dbo.Businesses'
Any help will be greatly appreciated.
my connection strings look like this:
<add name="ApplicationServices"
connectionString="Data Source=.;
Initial Catalog=MyDbName;
Persist Security Info=True;
User ID=sa;
Password=mypassword"
providerName="System.Data.SqlClient"
/>
<add name="MyEntities"
connectionString="data source=.;
Initial Catalog=MyDbName;
Persist Security Info=True;
User ID=sa;
Password=mypassword"
providerName="System.Data.SqlClient"
/>
Upvotes: 3
Views: 6662
Reputation: 21
I've just spent 2 hours trying to solve this problem, and i finally got it)
Just let the EF create your database first (using CodeFirst method), and then use aspnet_regsql.exe to add membership tables to the database.
It doesn't work the other way around :(
I have added EF DbContext to the home page controller, so that EF would create the database at the first launch of the site.
namespace Rem.Controllers
{
public class HomeController : Controller
{
DataEntities db = new DataEntities(); <--------------
public ActionResult Index()
{
ViewBag.Message = "Домашняя страница";
City c = db.Cities.Find(5); <--------------------
return View();
}
public ActionResult About()
{
ViewBag.Message = "Описание сайта";
return View();
}
public ActionResult Contact()
{
ViewBag.Message = "Контактные данные";
return View();
}
}
}
After that i just ran the aspnet utility.
To avoid duplication of connection strings i pass connectionString name to DbContext constructor in my DataContext derived class constructor:
namespace Rem.Models
{
public class DataEntities : DbContext
{
public DbSet<User> Users { get; set; }
public DbSet<City> Cities { get; set; }
public DataEntities():base(@"dbcs") { ; } <----------
}
}
Upvotes: 2
Reputation: 21366
http://www.codeproject.com/KB/web-security/EFMembershipProvider.aspx this is good example which implements custome membership provider with entity framework. And read this first Can I use Entity Framework with ASP.NET Membership?
Upvotes: 2