I'm working for the first time with Membership Reboot and I have custom class. I added a new property called Middle Name. How can I do the EF Migration on this to get it updated?
public class CustomUser : RelationalUserAccount
{
[Display(Name = "First Name")]
public virtual string FirstName { get; set; }
[Display(Name = "Last Name")]
public virtual string LastName { get; set; }
[Display(Name = "Middle Name")]
public virtual string MiddleName { get; set; }
public virtual int? Age { get; set; }
}
public class CustomUserAccountService : UserAccountService<CustomUser>
{
public CustomUserAccountService(CustomConfig config, CustomUserRepository repo)
: base(config, repo)
{
}
}
public class CustomUserRepository : DbContextUserAccountRepository<CustomDatabase, CustomUser>
{
public CustomUserRepository(CustomDatabase ctx)
: base(ctx)
{
}
}
Open package manager console
Run Enable-Migrations command for your project
Add an initial migration before changing any properties to set the initial state in your project
add-migration -Name Initial
Create the initial table structure in the database.
Update-Database
Add the MiddleName property in the customUser class
Add new migration for the changes you have done.
add-migration -Name middleName_added
Update the database to reflect the new changes in database
Update-Database
Run steps 5-7 as you updating the properties of CustomUser
Related
I want to know why I cant add a new table into my existing database.
I've created a new model:
public class RegisterAccount
{
public string Username { get; set; }
public string Password { get; set; }
public string ConfirmPassword { get; set; }
}
Added the necessary Dbset
public DbSet<RegisterAccount> RegisterAccount {get; set;}
I've typed in the PMC:
Add-Migration, then Update-Database but I can't see the new table in the database?
I've restarted SSMS and refreshed everything, am i missing something?
I got an issue with the migration system in EntityFramework Core 2.0, it seems to work only the first time with a database... :
When I use the command : add-migration InitialMigration
the migration code is generated, all works fine. But, if I use database-update, nothing happens...
If I change the database name in my Connection String with a random name (just add 123 for exemple), then the command database-update works and execute the migration in this new database...
Same problem if I try to make an other migration. Nothing happens, I have to change the database name again in order to generate the migration code.
DbContext :
public class TestProjectDbContext : DbContext
{
public TestProjectDbContext(DbContextOptions<TestProjectDbContext> options) : base(options)
{
}
public DbSet<User> User { get; set; }
}
User Class (namespace TestProject.Data.Models)
public class User
{
public int Id { get; set; }
public string LastName { get; set; }
public string FirstName { get; set; }
public string Login { get; set; }
public string Email { get; set; }
public DateTime Created { get; set; }
}
Connection string format (appsettings.json)
{
"ConnectionStrings": {
"DefaultConnection": "Server=IPADRESS;Database=TestProject;User Id=xxxxxx; Password=xxxxxx;MultipleActiveResultSets=True"
},
Any idea..? thanks :)
After a second look I believe that you're running "database-update" instead of "Update-Database"
Your code works for me as shown below.
I just grabbed your code and tested it myself with note of the service initialization in Startup.cs:
var connection = #"Server=(localdb)\myhost;Database=myTestDb;Trusted_Connection=True;";
services.AddDbContext<aDbContext>(options => options.UseSqlServer(connection));
I have a problem with "Add-Migration" instruction when the Table definition is in a new DbContext Class.
I created a new ASP.NET MVC 4 Application in Visual Studio 2012.
I ran the "Enable-Migrations", "Add-Migration mig1", "Update-Database". Everything was smooth.
Than, I added a new class inheriting the DbContext to the Models folder. I was hoping that "Add-Migration mig2" will notice the new table definition. But it does not.
Any ideas why?
namespace MvcApplication4.Models
{
public class CmsContext: DbContext
{
public CmsContext()
: base("DefaultConnection")
{
}
public DbSet<CustomItem> CustomItems { get; set; }
}
[Table("CustomItems")]
public class CustomItem
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public int Ordinal { get; set; }
public String Title { get; set; }
public String Content { get; set; }
public String FilePath { get; set; }
}
}
Multi-tenancy is not supported in EF5 but is supported in EF6 where you can specify a context for which you want to enable migrations for like this:
Enable-Migrations -ContextTypeName {NameOfTheContextType}
See the migrations multi-tenancy feature spec on the Entity Framework codeplex site.
I m just using EF 5.0 and I've recreated a very simple DbContext that was working as is with EF 4.1.
Here context and model
public class AgenciesDatabaseContext : DbContext
{
public DbSet<Agency> Agencies { get; set; }
}
[Table("QryAgency")]
public class Agency
{
[Key]
public string CardCode { get; set; }
public string DisplayName { get; set; }
public string CardFName { get; set; }
public string Address { get; set; }
public string ZipCode { get; set; }
public string City { get; set; }
}
I set in global.asax initializer for this context as null because the table already exists
Database.SetInitializer<ExtranetCentralizerContext>(null);
Here's the connection string in web.config :
<add name="AgenciesDatabase" providerName="System.Data.SqlClient" connectionString="..."/>
When I try to use the DbContext in the repository I get this error :
InnerException = {"Invalid column name '...'.\r\n Invalid column name '...'.\r\nInvalid column name '...'."}
It's strange because I could see that there is not connection made to my database.
What I don't understand is that I can make it work if I pass the connection string to the context like this :
public class AgenciesDatabaseContext : DbContext
{
public DbSet<Agency> Agencies { get; set; }
public AgenciesDatabaseContext ()
: base("AgenciesDatabase")
{
}
}
There everything work fine. So my question is : isn't EF suposed to use the connection string that matches it's name (in this case AgenciesDatabase) ??? What makes it fail in this case ?
in your app.config the name should be AgenciesDatabaseContext, not only AgenciesDatabase.
I have a strange problem with code first:
In project my entities looks like that and code first was doing migrations fine.
public class MyEntity
{
public Guid Id { get; set; }
public string Name { get; set; }
}
I decided to add an interface to my project like this one
public interface IEntity
{
Guid Id { get; set; }
bool IsDeleted { get; set; }
}
My new class now looks like that:
public class MyEntity : IEntity
{
public Guid Id { get; set; }
public string Name { get; set; }
bool IsDeleted { get; set; }
}
Now, if I try a migration with Code First, instead of adding a column here is what is doing code first:
DropForeignKey
DropIndex
Tries to CreateTable MyEntity table and breaks telling me this table already exists
Any idea why code first tries to do that?
Using EF5 I couldn't get it to repro. In general with migrations the order in which commands are executed is important so as the metadata is consistent between the database and models.
The correct order for these operations would be to either:
a) Run Enable-Migrations, Add-Migration then Update-Database before populating the database (i.e. running the app) then Add-Migration and Update database after each model change before running the app
b) run the app, Enable-Migrations, change the model, Add-Migrations, Update-Database, again making sure to run update the database after each model change is made
also note I am using a very simple DbContext that looks as such:
public class EntityContext : DbContext
{
public DbSet<MyEntity> MyEntities { get; set; }
}