迁移不工作,我希望... Asp.net EntityFramework [英] Migration not working as I wish... Asp.net EntityFramework

查看:104
本文介绍了迁移不工作,我希望... Asp.net EntityFramework的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习这个教程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 具有指定的连接
DefaultConnection )。另一个(电影...)没有 - 那个
意味着另一个连接将被命名为 MovieDbContext - 它
尝试找到它你的配置文件 - 如果它不成功 - 它使用
你的默认'提供者'找到Db - 并且它创建一个新的Db
在那里。虽然您的UserContext可能已经在.config中有一个已定义的连接,而且该工作原理。


将全部移动到UserContext - 或调整连接。请参阅附件



注意:



拥有多个DbContext的是所谓的<强>多租户迁移 - 目前尚不支持(针对EF5) - 而且为EF6而设计 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 tables

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.

Specifically your UserContext has a connection specified (DefaultConnection). The other one (Movie...) does not - that means the other connection will be named MovieDbContext - 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.

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 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆