Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to create an automatic table with fluentmigrator? #1809

Closed
suatsuphi opened this issue May 20, 2024 · 5 comments
Closed

Is it possible to create an automatic table with fluentmigrator? #1809

suatsuphi opened this issue May 20, 2024 · 5 comments

Comments

@suatsuphi
Copy link

Hi,

We have a sample table like below. Is it possible to create the following tables with migration? Is there a command that all indexes and constraints will be automatic? Isn't it very unnecessary to define them one by one for initial?

[MigrationVersion("2024-05-15 14:00:00", "Initial")]
internal class Initial : Migration
{
       if (!Schema.Table(nameof(Post)).Exists())
       {
              Create.Table(nameof(Post)).Automatic(Post);
       }
       if (!Schema.Table(nameof(Tag)).Exists())
       {
              Create.Table(nameof(Tag)).Automatic(Tag);
       }
       if (!Schema.Table(nameof(PostTag)).Exists())
       {
              Create.Table(nameof(PostTag)).Automatic(Post);
       }
}

using System.Collections.Generic;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Microsoft.EntityFrameworkCore;
using System.ComponentModel.DataAnnotations;

namespace Domain
{
    public class Post
    {
        public int Id { get; set; }

        [StringLength(400)]
        [Required]
        public string Name { get; set; }
        public List<PostTag> PostTags { get; } 
        public List<Tag> Tags { get; } 
    }

    public class Tag
    {
        public int Id { get; set; }

        [StringLength(400)]
        [Required]
        public string Name { get; set; }
        public List<PostTag> PostTags { get; } 
        public List<Post> Posts { get; } 
    }

    internal class PostTagMap : IEntityTypeConfiguration<PostTag>
    {
        public void Configure(EntityTypeBuilder<PostTag> builder)
        {
            builder
                .HasOne(a => a.Tag)
                .WithMany(a => a.PostTags)
                .HasForeignKey(c => c.PostsId)
                .OnDelete(DeleteBehavior.ClientSetNull);

            builder
               .HasOne(a => a.Tag)
               .WithMany(a => a.PostTags)
               .HasForeignKey(c => c.TagsId)
               .OnDelete(DeleteBehavior.ClientSetNull);
        }
    }

    [Index(nameof(PostsId), Name = "IX_PostTag_PostsId")]
    [Index(nameof(TagsId), Name = "IX_PostTag_PostsId")]
    [Index(nameof(DisplayOrder), Name = "IX_PostTag_PostsId")]
    [Index(nameof(PostsId), nameof(TagsId), IsUnique = true, Name = "IX_PostTag_PostsId_TagsId")]
    public class PostTag
    {
        public int Id { get; set; } 
        public int DisplayOrder { get; set; }
        public int PostsId { get; set; }
        public int TagsId { get; set; }
        public Post Post { get; set; } = null!;
        public Tag Tag { get; set; } = null!;
    }
}

@jzabroski
Copy link
Collaborator

If I understand your request, you want something similar to EF6 Automatic Migrations, since EFCore does not support Automatic Migrations. Is that right?

@suatsuphi
Copy link
Author

Imagine that you write an additional project to an existing project, you are not the first to run the project. so you create additional tables with migration.

@jzabroski
Copy link
Collaborator

jzabroski commented Jul 2, 2024

Sorry, I did not understand your two sentences. I think you are looking for something like Quentin Starin's EasyMigrator, though. It is linked in our third party library contributions, but it has not been kept up to date for .NET 5+.

@suatsuphi
Copy link
Author

thanks I meant something like that EasyMigrator, but I mean exactly this Create.Table<Poco>(); should be enough. it would be great if these definitions could be made in the class....

@jzabroski
Copy link
Collaborator

@suatsuphi I suspect you didn't read the full readme for EasyMigrator. See: https://github.com/qstarin/EasyMigrator?tab=readme-ov-file#fluentmigrator

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants