【发布时间】:2013-01-10 10:26:28
【问题描述】:
我有一些旧表,我通过逆向工程将它们添加到项目中,以将它们用作code first。
类似这样的:
public class User
{
public string Name { get; set; }
public int Id { get; set; }
}
public class Profile
{
[Key, ForeignKey("User")]
public int Id { get; set; }
public string Name { get; set; }
public virtual User User { get; set; }
}
public class EFDbContext : DbContext
{
public DbSet<User> Users{ get; set; }
public DbSet<Profile> Profiles { get; set; }
}
如果我的旧表 User 存在,那么 Entity Framework 不会创建其他表,例如 Profile 表。
我的问题如果不存在,我如何指定应该创建哪个表?
【问题讨论】:
-
您是说如果表“用户”存在,那么 EF 不会创建您想要的、尚不存在的表(如“配置文件”)?我不确定您所说的“不应操纵哪个表”是什么意思。
-
是的,这就是我所说的或我看到的,至少。我的最后一句话不正确,对不起。
-
@Blazi,您是将数据库优先与代码优先混合,还是您的项目纯粹是代码优先与数据库同步?
-
这里是代码优先(我通过实体框架插件从现有数据库创建了代码优先类)。但我也尝试过将代码优先与数据库优先混合。两种方法都没有成功。
标签: asp.net asp.net-mvc entity-framework ef-code-first