开发者

How to make sql membership provider and entity framework work using my database?

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 memb开发者_如何学运维ership 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" 
 />


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?


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") { ; } <----------
    }
}    
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜