Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
271 views
in Technique[技术] by (71.8m points)

c# - Migration not working as I wish... Asp.net EntityFramework

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...