爲了避免有兩個數據庫,我已將自己的實體映射到ASPNETDB.MDF,這是自動爲成員資格生成的。我在這篇文章下面以圖我的實體,以現有的數據庫:http://weblogs.asp.net/scottgu/archive/2010/08/03/using-ef-code-first-with-an-existing-database.aspx 據我瞭解所有我需要做的改變是DATABSE的上下文類這樣的名稱:將實體映射到ASPNETDB.MDF MVC3
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using UniversityApp.Models;
using System.Data.Entity.ModelConfiguration.Conventions;
namespace UniversityApp.DAL
{
public class ASPNETDB : DbContext
{
public DbSet<User> Users { get; set; }
public DbSet<Review> Reviews { get; set; }
public DbSet<Movie> Movies { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
}
}
我也有改變了連接字符串是這樣的:
<connectionStrings>
<add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient" />
<add name="MovieContext" connectionString="Data Source=|DataDirectory|aspnetdb.mdf" providerName="System.Data.SqlServerCe.4.0"/>
</connectionStrings>
一切似乎做工精細(方法舊數據庫,讀取和寫入的DATABSE,甚至初始化數據)。但是,當我打開ASPNETDB.mdf的連接時,我沒有看到我的表。我究竟做錯了什麼?
爲什麼你需要一些其他數據庫或aspnetdb.mdf中的成員表?您可以將ASP.NET成員資格/角色/配置文件添加到任何現有數據庫。只需用reg_sql.exe指定-d標誌 – mfanto 2012-04-24 19:07:04