为了避免拥有两个数据库,我已将我的实体映射到 ASPNETDB.MDF,它是为成员资格自动生成的。我正在关注这篇文章,以便将我的实体映射到现有数据库:http ://weblogs.asp.net/scottgu/archive/2010/08/03/using-ef-code-first-with-an-existing-database .aspx 据我了解,我需要做的只是更改上下文类的数据库名称,如下所示:
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>
一切似乎都工作正常(旧数据库的方法,读取和写入数据库,甚至初始化数据)。但是当我打开到 ASPNETDB.mdf 的连接时,我仍然看不到我的表。我究竟做错了什么?