Say I have an existing code-first entity (with multiple established migrations) that looks like this:
namespace OldNamespace
{
[Table("Widget", Schema = "summat")]
public class Widget
{
}
}
And I want to move it to a NewNamespace
in a C# sense (but not in a database table sense):
namespace NewNamespace
{
[Table("Widget", Schema = "summat")]
public class Widget
{
}
}
The model snapshot that is generated includes the full c# namespace of the entity, so it seems problematic that EF will know what an old/existing namespace will be after a migration?
protected override void BuildModel(ModelBuilder modelBuilder)
{
//...
modelBuilder.Entity("OldNamespace.Widget", b =>
{
}
}
Has anyone done this sort of thing? How can I do the refactor without having to "squash" all migrations? Is there a way to set up metadata so that EF can follow the namespace refactor?
EF Core version: 1.0.1
Update: A new ef core migration looks like this:
The model snapshot that is generated includes the full c# namespace of the entity, so it seems problematic that EF will know what an old/existing namespace will be after a migration?
This is not problematic. At the end of the day, these are just opaque strings used at design-time. The fact that're the same as your old namespace names is irrelevant.
Not been able to reproduce it.
Added Widget entity to Models folder:
namespace WebApplication1.Models
{
[Table("Widget", Schema = "summat")]
public class Widget
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
}
}
Created a migration. (dotnet ef migrations add AddWidget)
Got this:
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.EnsureSchema(
name: "summat");
migrationBuilder.CreateTable(
name: "Widget",
schema: "summat",
columns: table => new
{
Id = table.Column<int>(nullable: false)
.Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
Name = table.Column<string>(nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_Widget", x => x.Id);
});
}
Moved the entity to Domain.Entities folder.
namespace WebApplication1.Domain.Entities
{
[Table("Widget", Schema = "summat")]
public class Widget
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
}
}
Added another migration: dotnet ef migrations add MoveWidget
. Got this:
protected override void Up(MigrationBuilder migrationBuilder)
{
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With