迁移不工作,我想... Asp.net的EntityFramework [英] Migration not working as I wish... Asp.net EntityFramework
问题描述
我在学习本教程asp.net电影教程和我在我的模型2个简单的文件。
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; }
}
和另一种是客户模式,Visual Studio的给我:
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; }
到用户配置类,我得到的迁移没有改变....这是因为迁移只更新类是MovieDBContext内....所以我必须是这样的:
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}
}
问题是,在另一个项目中,我试图更新AccountModel并尝试这种解决方案,但它没有工作....有谁知道另一种解决办法?
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?
推荐答案
我觉得你的经历是一个连接的问题即可。 (请检查了这一点,因为我前面做了一个演练 - 和连接链接)结果
<一href=\"http://stackoverflow.com/questions/15730662/migration-does-not-alter-my-table/15734839#15734839\">Migration不会改变我的表(用于连接问题)结果
code首先创建表
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 tables
把它放进一个单独的DbContext - 你并不真的需要两个(至少我是这么认为的)。你只需要当你为'调整'的连接。
Put 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.
( DefaultConnection
)。另外一个(电影......)不 - 这
意味着其他连接将被命名为 MovieDbContext
- 它
试图找到它在你的配置文件 - 如果它没有成功 - 它使用
默认的供应商找到数据库 - 它创建一个新的数据库
那里。虽然你可能的UserContext已经在.config一个定义的连接 - 这一件作品。
Specifically your
UserContext
has a connection specified (DefaultConnection
). The other one (Movie...) does not - that means the other connection will be namedMovieDbContext
- and it tries to find it in your config file - if it doesn't succeed - it uses your default 'provider' to find the Db - and it creates a new Db there. While your UserContext probably already has a defined connection in the .config - and that one works.
只是全部转移到'的UserContext' - 或调整连接。见附件
Just move all to 'UserContext' - or adjust connections. see attached
注意:
拥有多个的DbContext-S是所谓的多租户迁移 - 它不支持作为的,但(对EF5) - 和即将到来的EF6的复合每个数据库的上下文中EF6 。
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.
对于'黑客',你可能会迫使添加迁移
运行 - 通过做类似添加迁移-ConfigurationTypeName Test.Migrations。 MovieConfiguration InitialMovie
- 并且是单独针对另一个 - 但不会在一个项目中工作(它会抱怨的迁移挂起或东西) - 如果你移动到两个不同的 - 你会一旦你运行Update打一迁移表'墙。
For a 'hack' you could force the Add-Migration
to run - by doing something like Add-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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!