Ignore schema attribute for SQLite during migration? - c#

I have a shared model that will be used to create both SqlServer and SQLite databases. I added the Table attribute onto each one of my model classes and supplied a schema:
[Table("Sites", Schema = "Common")]
When I migrate the SQLite database using context.Database.Migrate(), I get a NotSupportedException error. I know that SQLite doesn't support schemas, so I was wondering if there was an easy way to ignore the schema attribute during migrate?

Set the schema in code rather than by attribute. That way you can use some method/config to determine the schema, for example:
public class MyContext
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
if(UsingSqlLite)
{
modelBuilder.Entity<Site>().ToTable("Sites");
}
else
{
modelBuilder.Entity<Site>().ToTable("Sites", "Common");
}
}
//snip
}

Since you are using the same migration on both SQL Server and SQLite, you probably need to ignore the schema inside the migration.
protected override void Up(MigrationBuilder migrationBuilder)
{
if (migrationBuilder.ActiveProvider == "Microsoft.EntityFrameworkCore.Sqlite")
{
migrationBuilder.CreateTable(
name: "Sites",
...);
}
else
{
migrationBuilder.CreateTable(
name: "Sites",
schema: "Common",
...);
}
}

Related

Entity Framework Apply Migrations Programatically

I am trying to apply migrations through c# code using entity framework.
I want to drop certain tables(not all of them) and recreate programatically.
I have something like this:
try
{
await dbContext.Database.ExecuteSqlRawAsync("drop table if exists results");
await dbContext.GetInfrastructure().GetService<IMigrator().MigrateAsync("20230104163600_AfterSync");
}
catch (Exception e)
{
throw;
}
I try to run specific custom migration which contains only some of the entities of the DbContext.
Here's the migration:
[DbContext(typeof(TempDbContext))]
[Migration("20230104163600_AfterSync")]
public partial class AfterSync : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "results",
columns: table => new
{
id = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
groupId = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
},
constraints: table =>
{
table.PrimaryKey("pK_results", x => x.id);
});
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "results");
}
}
If I remove the line
await dbContext.Database.ExecuteSqlRawAsync("drop table if exists results");
It says:
Microsoft.Data.SqlClient.SqlException: 'There is already an object named 'results' in the database.'
If I remove the override of Down method in the migration and leave the previous line, the application just crashes.
Can someone help how I can drop and recreate only certain tables not all of them withtout using the database.Migrate() method. Thanks

EF-Core: Table "name" already exists - when trying to update database

ASP Core 3.1 - API. I'm using the latest version of Entity Framework Core.
I have created a table ToDoItem and a ToDoItemContext. After creating the initial migration, and running update-database. I now have that table in my database. I now added a new model called: ToDoItemDescription.
When I try to update the database after creating a new migration, I get the error:
Table 'todoitems' already exists
Further details: I have two contexts, and this is the command I ran:
update-database -context todoitemscontext
I also tried:
update-database -context todoitemscontext -migration AddDescription
Here is my full code:
Models:
public class TodoItem : IEntity
{
public long Id { get; set; }
public string Name { get; set; }
bool IsComplete { get; set; }
}
public class ToDoItemDescription
{
public int id { get; set; }
public string Description { get; set; }
//public int ToDoItemId { get; set; }
public TodoItem TodoItem { get; set; }
}
Context:
public class TodoItemsContext : DbContext
{
public TodoItemsContext(DbContextOptions<TodoItemsContext> options) : base(options) { }
public DbSet<TodoItem> TodoItems { get; set; }
public DbSet<ToDoItemDescription> TodoItemsDescription { get; set; }
}
Migrations:
[DbContext(typeof(TodoItemsContext))]
partial class TodoItemsContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder) {
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "3.1.9")
.HasAnnotation("Relational:MaxIdentifierLength", 64);
modelBuilder.Entity("project.Models.ToDoItemDescription", b => {
b.Property<int>("id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
b.Property<string>("Description")
.HasColumnType("longtext CHARACTER SET utf8mb4");
b.Property<long?>("TodoItemId")
.HasColumnType("bigint");
b.HasKey("id");
b.HasIndex("TodoItemId");
b.ToTable("TodoItemsDescription");
});
modelBuilder.Entity("project.Models.TodoItem", b => {
b.Property<long>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("bigint");
b.Property<bool>("IsComplete")
.HasColumnType("tinyint(1)");
b.Property<string>("Name")
.HasColumnType("longtext CHARACTER SET utf8mb4");
b.HasKey("Id");
b.ToTable("TodoItems");
});
modelBuilder.Entity("project.Models.ToDoItemDescription", b =>
{
b.HasOne("project.Models.TodoItem", "TodoItem")
.WithMany()
.HasForeignKey("TodoItemId");
});
#pragma warning restore 612, 618
}
public partial class TodoItems_Initial : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "TodoItems",
columns: table => new
{
Id = table.Column<long>(nullable: false)
.Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
Name = table.Column<string>(nullable: true),
IsComplete = table.Column<bool>(nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_TodoItems", x => x.Id);
});
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "TodoItems");
}
}
public partial class AddDescription : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "TodoItemsDescription",
columns: table => new
{
id = table.Column<int>(nullable: false)
.Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
Description = table.Column<string>(nullable: true),
TodoItemId = table.Column<long>(nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_TodoItemsDescription", x => x.id);
table.ForeignKey(
name: "FK_TodoItemsDescription_TodoItems_TodoItemId",
column: x => x.TodoItemId,
principalTable: "TodoItems",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
});
migrationBuilder.CreateIndex(
name: "IX_TodoItemsDescription_TodoItemId",
table: "TodoItemsDescription",
column: "TodoItemId");
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "TodoItemsDescription");
}
}
Thank you.
This happens if you have created the database upfront without migrations, for example by using DbContext.Database.EnsureCreated();.
This usually happens when you have a migration that creates a table and the required table is already present in your database so, when you update the database from classes in Migration, it will try to create a table and will fail because the Create command will not be executed as it already has that specific table.
So, in order to avoid the error, you might want to remove the migration class or comment the code in Up() method of that class so it doesn't execute that specific create command.
It could possible help people working with MySQL databases either on Linux and Windows
TL;DR;
I had to rename the table
__efmigrationshistory (note the lowercase) to
__EFMigrationsHistory (note the case)
so the command-line dotnet-ef database update managed to verify all the migrations present on the table __EFMigrationsHistory, and therefore, creating the new field on the table, say Tenant
More
I have to work on Linux, Windows, MacOs boxes. Primarily using Visual Studio code and .net core 3.1.xxx
I use the code-first approach. The MySQL database was firstly, create on the Windows box, where all the tables were created lower cased
Switching to the Linux box, I realized the case was important, so, say, table "tenant" was renamed to "Tenant", by hand.
Once I had to create a new field on the Tenant's c# class, I ran:
dotnet-ef migrations add new-ftpSettings-field and dotnet-ef database update, I got table "Order" already exists. Note I was trying to insert a new field to the "Tenant" table
After a lot of investigation and search, I decided to refresh the database again, and I saw "two suspicious tables" __efmigrationshistory and __EFMigrationsHistory.
I renamed the empty table __EFMigrationsHistory to like Table1 (as a backup), and thus renamed the table __efmigrationshistory to __EFMigrationsHistory
I ran the dotnet-ef database update and the field was properly added to the MySQL database.
*** Like you might have figured this out, running the command-line dotnet-ef database update on Linux was creating a new (and) empty table __EFMigrationsHistory to MySQL database while it had already, a lower cased table on __efmigrationshistory (the good one, created on my Windows box, with all the migrations).
*** This is my first contribution. Any advice is welcome!
Keep safe! Tchau/Au revoir!
I was working through the migration tutorial and had made a mistake sometimes around these steps
dotnet ef migrations add AddBlogCreatedTimestamp
dotnet ef database update
I did the following
deleted the files AddBlogCreatedTimestamp.Designer.cs and AddBlogCreatedTimestamp.cs
inside blogging.db in the table __EFMigrationsHistory i deleted the row that contains 2023__***__AddBlogCreatedTimestamp this was the migration step that failed.
I repeated the migration step dotnet ef migrations add ...
then manually added DropTable(...) to AddBlogCreatedTimestamp.Up()
only then i ran dotnet ef database update
This made sure that in an up-migration the tables would be deleted
Code manually changed
public partial class AddBlogCreatedTimestamp : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
// manually added
migrationBuilder.DropTable(name: "Posts");
migrationBuilder.DropTable(name: "Blogs");
// ... other lines that were created
}
// more other code ...
}
What i still not get is why this is needed. I am not aware to have used anything like EnsureCreated

Entity Framework 7 migration not creating tables

I am working with my first project using Entity Framework 7 and am connecting to a SQL Server where the Database is already created but there are no tables in it yet. I have created my DbContext and created a class, then set a DbSet<> inside my context. I ran the commands to enable migrations and create the first migration, then rand the command to update the database. Everything looked to work fine, no errors came up, but when I look at the database only the EFMigraitonsHistory table was created. When I look at the class that was created for the initial migration it is essentially blank. What am I doing wrong?
Commands I am running:
dnvm install latest -r coreclr
dnx ef migrations add MyFirstMigration
dnx ef database update
Context:
namespace JobSight.DAL
{
public class JobSightDBContext : DbContext
{
public DbSet<NavigationMenu> NavigationMenu { get; set; }
}
}
Table Class:
namespace JobSight.DAL
{
public class NavigationMenu
{
[Required, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Int16 ID { get; set; }
public string ControllerName { get; set; }
public string ActionName { get; set; }
public string ExternalURL { get; set; }
public string Title { get; set; }
public Int16? ParentID { get; set; }
public virtual NavigationMenu Parent { get; set; }
}
}
Startup.cs:
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddEntityFramework()
.AddSqlServer()
.AddDbContext<JobSightDBContext>(options =>
{
options.UseSqlServer(Configuration["Data:JobSightDatabase:ConnectionString"]);
});
}
Class for initial migration (autogenerated by EF):
namespace JobSight.WebUI.Migrations
{
public partial class Initial : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
}
protected override void Down(MigrationBuilder migrationBuilder)
{
}
}
}
Edit:
After doing what Poke has suggested this is my new auto-generated migration. The table is still not being created at the database level though.
namespace JobSight.WebUI.Migrations
{
public partial class MyFirstMigration : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "NavigationMenu",
columns: table => new
{
ID = table.Column<short>(nullable: false)
.Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
ActionName = table.Column<string>(nullable: true),
ControllerName = table.Column<string>(nullable: true),
ExternalURL = table.Column<string>(nullable: true),
ParentID = table.Column<short>(nullable: true),
Title = table.Column<string>(nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_NavigationMenu", x => x.ID);
table.ForeignKey(
name: "FK_NavigationMenu_NavigationMenu_ParentID",
column: x => x.ParentID,
principalTable: "NavigationMenu",
principalColumn: "ID",
onDelete: ReferentialAction.Restrict);
});
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable("NavigationMenu");
}
}
}
You need to set up the entity in your database context first. At the very least, you would need to do this:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<NavigationMenu>();
}
The problem with your migrations was a bit hidden in your project layout. So what you have is a JobSight.DAL project that contains the entities and the database context. And then you have a project JobSight.WebUI which is the actual ASP project containing the Startup.cs with the database setup.
This is causing problems because by default EF will just assume to find everything in the current assembly. So if you are launching the ef command from your web project, it will create the migrations in there even if the context is in another project. But when you’re then trying to apply the migration, EF will not find it since it will only look in the context’s project.
So to fix this, you need to create the migrations in the DAL project. You can do that by specifying the project when you call the ef command:
dnx ef migrations add Example -p JobSight.DAL
You can verify that this worked by running dnx ef migrations list afterwards. This should now return the Example migration; previously, that command didn’t return anything: It could not find a migration which is the reason why the update command only said Done (without applying the migration) and the database wasn’t created. So if you now get the migration there, you can then apply it using:
dnx ef database update
Note that since the migration is now created in the DAL project, you need to add a reference to EntityFramework.MicrosoftSqlServer there, otherwise the project will not compile. You need to do that before you can run the list command above.
Finally, for some more information about this, see this issue.
Although this is not the answer to the original question, I post my answer here because it might help someone who has a similar problem. My problem was also that the tables were not created, but dotnet ef migrations add InitialCreate did create 3 .cs files in the Migrations folder. But dotnet ef database update only created the MigrationsHistory table and dotnet ef migrations list did not return any migrations.
It turned out that the problem was that the Migrations folder was excluded from the Visual Studio project. Once I included it again, everything worked fine.
I had the same problem and this is how I resolved it
I deleted my database in SQL
I changed the name I used for the previous migration. I changed from "Add-Migration InitialCreate" to "Add-Migration NewName"

EF5 Code First: Detect if it creates a database so I can run ALTER statements

I am new with Entity Framework 5. Our team is using Code First workflow.
Before I'll start with my main question, let me first show you what I have tried (the ultimate comment of all time :D).
public class MyDBContext : CDBContext
{
public MyDBContext() : base(connString) { }
public MyDBContext(string connStr) : base(connStr) { }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// removes some conventions
modelBuilder.Conventions.Remove<ManyToManyCascadeDeleteConvention>();
// ........
// model configurations which contains mappings
modelBuilder.Configurations.Add(new AccountConfiguration());
// ........
// calls base OnModelCreating
base.OnModelCreating(modelBuilder);
}
// list of all Entity
public DbSet<Account> Account { get; set; }
}
MyDBContext is the class I have created that inherits from CBDContext that contains override methods and which also inherits from DBContext. One of the problems I have encountered is that entity framework doesn't handle field uniqueness. I have already read the article on Configuring/Mapping Properties and Types with the Fluent API on their site and I can't find any configuration to set a property into unique.
So what I did in order to set the field unique is to manually run several ALTER sql statements during creation,
using (MyDBContext _context = new MyDBContext(connString))
{
if (_context.Database.CreateIfNotExists())
{
_context.Database.ExecuteSqlCommand("ALTER TABLE Account ADD CONSTRAINT UQ_Account_AccountNumber UNIQUE(AccountNumber)");
_context.Database.ExecuteSqlCommand("ALTER TABLE Account ADD CONSTRAINT UQ_Account_GUID UNIQUE(GUID)");
// .... more on this in the following lines ...
}
}
My Questions:
Am I right that entity framework don't have any configuration or data annotations to set the field unique?
Is there a way to detect or know during runtime if EF creates a database or not so I can move or hide this statement if (_context.Database.CreateIfNotExists()) somewhere to an available method that can be overriden?
What I really want is to remove if (_context.Database.CreateIfNotExists()) from the using statemnt and put it somewhere else or inside MyDBContext so my code will look like this,
using (MyDBContext _context = new MyDBContext(connString))
{
Account _acc = new Account()
// ...Account properties ...
_context.Account.Add(_acc);
_context.SaveChanges();
}
Thanks.
You should take a look at Code First Migrations, more specific at the Data Motion / Custom SQL and later sections - this is might the way to achieve your desired result. Your migration class can look like this:
public partial class AddUniqueConstrains : DbMigration
{
public override void Up()
{
Sql("ALTER TABLE Account ADD CONSTRAINT UQ_Account_AccountNumber UNIQUE(AccountNumber)");
Sql("ALTER TABLE Account ADD CONSTRAINT UQ_Account_GUID UNIQUE(GUID)");
}
public override void Down()
{
Sql("ALTER TABLE Account DROP CONSTRAINT UQ_Account_AccountNumber UNIQUE");
Sql("ALTER TABLE Account DROP CONSTRAINT UQ_Account_GUID");
}
}
You can also explore other options described in answers to this question: Unique Constraint in Entity Framework Code First
If you don't use (or cannot use) EF migrations you can use custom initializer as mentioned in this answer. The custom initializer will execute a Seed method after creating the database = only once when database doesn't exist. If you need to incrementally develop the database initializer itself will not help you (that is what migrations are for).

How to rename a database column in Entity Framework 5 Code First migrations without losing data?

I got the default ASP.NET MVC 4 template successfully running with EF 5.0 Code First Migrations. However, when I update a model property name, the corresponding table column data is dropped by EF 5.0.
Is it somehow possible to rename the table column without dropping data in an automated way?
Manually edit the Up and Down methods of the migration to use the RenameColumn method to replace the AddColumn and DropColumn that it automatically generates for you.
As already said, replace the AddColumn and DropColumn that is automatically generated with RenameColumn.
Example:
namespace MyProject.Model.Migrations
{
using System;
using System.Data.Entity.Migrations;
public partial class RenameMyColumn : DbMigration
{
public override void Up()
{
// Remove the following auto-generated lines
AddColumn("dbo.MyTable", "NewColumn", c => c.String(nullable: false, maxLength: 50));
DropColumn("dbo.MyTable", "OldColumn");
// Add this line
RenameColumn("dbo.MyTable", "OldColumn", "NewColumn");
}
public override void Down()
{
// Remove the following auto-generated lines
AddColumn("dbo.MyTable", "OldColumn", c => c.String(nullable: false, maxLength: 50));
DropColumn("dbo.MyTable", "NewColumn");
// Add this line
RenameColumn("dbo.MyTable", "NewColumn", "OldColumn");
}
}
}
You can get the migration to call RenameColumn for you if you do this:
[Column("NewName")]
public string OldName { get; set; }
Here is the generated migration:
public override void Up()
{
RenameColumn(table: "Schema.MyTable", name: "OldName", newName: "NewName");
}
public override void Down()
{
RenameColumn(table: "Schema.MyTable", name: "NewName", newName: "OldName");
}
If you want your property and DB column to be the same name, you can rename the property later and remove the Column attribute.
you have 2 steps to rename column in code first migration
The first step,you add ColumnAttribute above your column which are changed, and then update-database command
[Column("Content")]
public string Description { set; get; }
The second step,
add-migration yournamechange command in order to create a partial class DbMigration.
add into up and down method here
RenameColumn("yourDatabase","name","newName");
public override void Up()
{
RenameColumn("dbo.your_database", "oldColumn",
"newColumn");
}
public override void Down()
{
RenameColumn("dbo.your_database", "newColumn",
"oldColumn");
}
Because when you connect, your database and model class will communicate via name_column at database and name_type at property method in model above.
Now, this answer is based on my knowledge of EF4.3, so I hope the migrations work roughly the same in EF5 :) After you've created a migration, you should be able to add code in the Up and Down methods, between the dropping of the old property and the creation of the new property. This code should move the property data in the correct direction. I've solved it with the SQL() method in where you can enter raw SQL to perform the data move.
In the Up method of the migration:
SQL("update [TheTable] set [NewColumn] = [OldColumn]");
and in the Down() method:
SQL("update [TheTable] set [OldColumn] = [NewColumn]");
The disadvantage of this approach is that you might couple your code with the database you're working with at the moment (since you're writing raw DB-specific SQL). There might be other methods available for data movement as well.
More info available here: MSDN
Adding to Josh Gallagher's answer:
In some places the sp_RENAME syntax is described like this:
sp_RENAME 'TableName.[OldColumnName]' , '[NewColumnName]', 'COLUMN'
However, that will actually include the brackets in the new column name.
DbMigration's RenameColumn() method will probably do the same, so avoid using brackets when specifying the new column name.
Also, the auto-generated commands in Up() & Down() include DropPrimaryKey() and AddPrimaryKey() if the column being renamed is part of the primary key. These are not needed when using RenameColumn(). The underlying sp_RENAME automatically updates the primary key if needed.
In Entity Framework Core 3.1.1, If you need to rename a column -
Add migration as below
migrationBuilder.RenameColumn(
name: "oldname",
table: "tablename",
newName: "newname",
schema: "schema");
like +Josh Gallagher said, you can use up() for doing things like:
public override void Up()
{
RenameColumn("dbo.atable","oldname","newname");
AddColumn("dbo.anothertable", "columname", c => c.String(maxLength: 250));
}
i've found this a good help in gettin into migration ;)

Categories