You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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!;
}
}
The text was updated successfully, but these errors were encountered:
If I understand your request, you want something similar to EF6 Automatic Migrations, since EFCore does not support Automatic Migrations. Is that right?
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.
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?
The text was updated successfully, but these errors were encountered: