Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How might I move an existing entity into a different c# namespace and maintain migrations?

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:

enter image description here

like image 920
el2iot2 Avatar asked Sep 16 '25 18:09

el2iot2


2 Answers

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.

like image 109
bricelam Avatar answered Sep 18 '25 10:09

bricelam


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)
{
}
like image 20
Karel Tamayo Avatar answered Sep 18 '25 10:09

Karel Tamayo