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

亡梦爱人 提交于 2019-12-04 13:16:55
Jayantha Lal Sirisena

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") { ; } <----------
    }
}    
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!