How to make sql member provider and entity structure work with my database?

I have a .NET MVC 3 project where I use a sql member provider and entity infrastructure.

I created a new database on my SQL server and created all the objects for the membership provider using aspnet_regsql.exe , I changed the connection string and it works fine.

Now I want to use the framework entity for db interaction. I use Code-First, so I created a simple entity class, and I can save the data in db and get it.

The problem is that the place where the entity infrastructure stores data is mistery. I cannot find any database in my project, and there are no new tables in my SQL database.

If I add a second connection string to web.config (the first for a membership provider), my infrastructure entity stops working (I assume that it should create new tables in my database), but it does not.

My entity class is as follows:

 public class MyEntities : DbContext { public DbSet<Business> Businesses { get; set; } } 

And I get an Invalid object name 'dbo.Businesses'

Any help would be greatly appreciated.

my connection strings are as follows:

 <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" /> 
+3
source share
2 answers

http://www.codeproject.com/KB/web-security/EFMembershipProvider.aspx This is a good example that a custome membership provider with an entity infrastructure implements. And read this first. Can I use Entity Framework with ASP.NET membership?

+2
source

I just spent 2 hours trying to solve this problem and finally got it)

Just let EF create your database first (using the CodeFirst method) and then use aspnet_regsql.exe to add the membership tables to the database.

This does not work the other way around :(

I added EF DbContext to the home page controller, so that EF will create a database the first time the site starts.

 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 duplicate connection strings, I pass the name connectionString to the DbContext constructor in my constructor for the derived DataContext class:

 namespace Rem.Models { public class DataEntities : DbContext { public DbSet<User> Users { get; set; } public DbSet<City> Cities { get; set; } public DataEntities():base(@"dbcs") { ; } <---------- } } 
+2
source

Source: https://habr.com/ru/post/1433004/


All Articles