问题描述
我正在学习这个教程asp.net电影教程,我有两个简单的文件在我的模型。 public class Movie
{
public int ID {get;组;
[必需]
公共字符串标题{get;组; }
}
public class MovieDBContext:DbContext
{
public DbSet< Movie>电影{get;组; }
}
另一个是帐户Visual Studio给我的模型:
public class UsersContext:DbContext
{
public UsersContext()
:base(DefaultConnection)
{
}
public DbSet< UserProfile> UserProfiles {get;组;
}
[表(UserProfile)]
public class UserProfile
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption。身份)]
public int UserId {get;组; }
public string UserName {get;组; }
}
问题是....如果我添加一个propety
public int aaa {get;组;对于UserProfile类,$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $迁移只更新MovieDBContext里面的类....所以我必须添加一些东西:
- 并且另外为另一个 - 但这不会在一个单一的项目(它会抱怨迁移等待或某事) - 如果您移动到两个不同的位置,则在运行更新后,您将会打到一个迁移表墙。public class MovieDBContext:DbContext
{
public DbSet< Movie>电影{get;组; }
public DbSet< UserProfile>用户{get; set}
}
问题是在另一个项目我试图更新AccountModel并尝试这个解决方案,但它没有工作....有没有人知道另一个解决方案?
解决方案我认为你所遇到的是一个'连接'问题。 (请查看我之前做过的演练,连接链接)
(用于连接问题)
将其放入单个DbContext 中 - 真的需要两个(至少我想是这样)。您只需要调整连接即可。
将全部移动到UserContext - 或调整连接。请参阅附件
注意:
拥有多个DbContext的是所谓的多租户迁移 - 目前尚不支持(针对EF5) - 而且为EF6而设计。
它在同一个数据库中根本不能有两个迁移表,因为每个迁移/上下文都需要自己的。
对于黑客,您可以通过执行类似<$ c来强制执行
Add-Migration
$ c> Add-Migration -ConfigurationTypeName Test.Migrations.MovieConfiguration InitialMovie
另一个解决方案是关闭迁移(一个),但我没有尝试 - 或者运行两个上下文连接到两个数据库 - 你不想真正想要的数据库,如果你想在之间进行沟通。无论如何 - 这仅仅是为了学术目的,这不是你的情况。
I was studying this tutorial asp.net Movie Tutorial and I have 2 simple files in my model.
public class Movie { public int ID { get; set; } [Required] public string Title { get; set; } } public class MovieDBContext : DbContext { public DbSet<Movie> Movies { get; set; } }
And the other one is the Account Model that Visual Studio gives me :
public class UsersContext : DbContext { public UsersContext() : base("DefaultConnection") { } public DbSet<UserProfile> UserProfiles { get; set; } } [Table("UserProfile")] public class UserProfile { [Key] [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)] public int UserId { get; set; } public string UserName { get; set; } }
The problem is.... If I add a propety
public int aaa { get; set; }
to the UserProfile class, I get no changes in the migration....This happens because the migration only updates class that are inside the MovieDBContext.... So I must add something like:
public class MovieDBContext : DbContext { public DbSet<Movie> Movies { get; set; } public DbSet<UserProfile> User{get;set} }
The problem is that in another project I am trying to update the AccountModel and tried this solution but it didnt work.... Does anyone know another solution?
解决方案I think what you're experience is a 'connection' problem. (please check this out for a walkthrough I made earlier - and the connections link)
Migration does not alter my table (for connection issue)
Code first create tablesPut it into a single DbContext - you don't really need two (at least I think so). You just need to 'adjust' the connections when you do.
Just move all to 'UserContext' - or adjust connections. see attached
Note:
Having multiple DbContext-s is what's called multi-tenant migrations - it's not supported as of yet (for EF5) - and is coming for EF6 Multiple Contexts per Database in EF6.
It simply cannot have 'two migration tables' in the same database - as each migration/context requires its own.
For a 'hack' you could force the
Add-Migration
to run - by doing something likeAdd-Migration -ConfigurationTypeName Test.Migrations.MovieConfiguration InitialMovie
- and separately for the other one - but that won't work in a single project (it'd complain for migrations pending or something) - and if you move to two different - you'll hit 'one migration table' wall once you run the Update.The other solution would be to turn off migrations (for one) but I haven't tried that - or run two context connecting to two databases - which you don't really want if you want to 'communicate' in between sort of. Either way - this is just for 'academic purposes' purely - it's not for your case.
这篇关于迁移不工作,我希望... Asp.net EntityFramework的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!