Скрипты миграции не отражают взаимосвязи, как определено в Fluent API - PullRequest
0 голосов
/ 21 января 2019

У меня есть следующее:

public class Comment : IEntity
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    [JsonProperty("id")]
    public Guid Id { get; set; }

    [JsonProperty("userId")]
    public Guid UserId { get; set; }

    [JsonProperty("text")]
    public string Text { get; set; }

    [JsonProperty("eventId")]
    public Guid EventId { get; set; }

    [JsonProperty("date")]
    public DateTimeOffset DateCreated { get; set; }

    [JsonProperty("id")]
    public DateTimeOffset DateModified { get; set; }

    public virtual Event Event { get; set; }

}
public class Event : IEntity
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    [JsonProperty("id")]
    public Guid Id { get; set; }

    [JsonProperty("description")]
    public string Description { get; set; }

    [JsonProperty("date")]
    public DateTimeOffset Date { get; set; }

    [JsonProperty("distance")]
    public int Distance { get; set; }

    [JsonProperty("verticalAscend")]
    public int VerticalAscend { get; set; }

    [JsonProperty("userId")]
    public Guid UserId { get; set; }

    //attending
    public DateTimeOffset DateCreated { get; set; }
    public DateTimeOffset DateModified { get; set; }

    [JsonProperty("user")]
    public virtual User User { get; set; }

    [JsonProperty("comments")]
    public virtual ICollection<Comment> Comments { get; set; }

    [JsonProperty("attending")]
    public virtual ICollection<User> AttendingList { get; set; }
}
public class User : IEntity
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    [JsonProperty("id")]
    public Guid Id { get; set; }

    [JsonProperty("profilePicUrl")]
    public string ProfilePicUrl { get; set; }

    [JsonProperty("name")]
    public string Name { get; set; }

    [JsonProperty("surname")]
    public string LastName { get; set; }

    [JsonProperty("email")]
    public string Email { get; set; }

    public DateTimeOffset DateCreated { get; set; }
    public DateTimeOffset DateModified { get; set; }

    public virtual ICollection<Event> AttendingEvents { get; set; }
    public virtual ICollection<Event> Events { get; set; }
}

У меня есть следующая Fluent API конфигурация в моем классе EventConfig, которая наследуется от EntityTypeConfiguration<Event>.

public class EventConfig : EntityTypeConfiguration<Event>
{
    public EventConfig()
    {

        HasRequired(r => r.User)
            .WithMany(m => m.Events)
            .HasForeignKey(f => f.UserId)
            .WillCascadeOnDelete(false);

        HasMany(x => x.Comments)
            .WithRequired(c => c.Event)
            .HasForeignKey(c => c.EventId)
            .WillCascadeOnDelete(false);

        HasMany(x => x.AttendingList)
            .WithMany(u => u.AttendingEvents)
            .Map(m =>
            {
                m.MapLeftKey("EventId");
                m.MapRightKey("UserId");
                m.ToTable("AttendeeList");
            });
    }
}

МиграцияСценарии, как следует, неверны в соответствии с моей настройкой Fluent API.

public partial class initial : DbMigration
{
    public override void Up()
    {
        CreateTable(
            "dbo.Comments",
            c => new
                {
                    Id = c.Guid(nullable: false, identity: true),
                    UserId = c.Guid(nullable: false),
                    Text = c.String(),
                    EventId = c.Guid(nullable: false),
                    DateCreated = c.DateTimeOffset(nullable: false, precision: 7),
                    DateModified = c.DateTimeOffset(nullable: false, precision: 7),
                })
            .PrimaryKey(t => t.Id)
            .ForeignKey("dbo.Events", t => t.EventId, cascadeDelete: true)
            .Index(t => t.EventId);

        CreateTable(
            "dbo.Events",
            c => new
                {
                    Id = c.Guid(nullable: false, identity: true),
                    Description = c.String(),
                    Date = c.DateTimeOffset(nullable: false, precision: 7),
                    Distance = c.Int(nullable: false),
                    VerticalAscend = c.Int(nullable: false),
                    UserId = c.Guid(nullable: false),
                    DateCreated = c.DateTimeOffset(nullable: false, precision: 7),
                    DateModified = c.DateTimeOffset(nullable: false, precision: 7),
                    User_Id = c.Guid(),
                    User_Id1 = c.Guid(),
                })
            .PrimaryKey(t => t.Id)
            .ForeignKey("dbo.Users", t => t.User_Id)
            .ForeignKey("dbo.Users", t => t.User_Id1)
            .ForeignKey("dbo.Users", t => t.UserId, cascadeDelete: true)
            .Index(t => t.UserId)
            .Index(t => t.User_Id)
            .Index(t => t.User_Id1);

        CreateTable(
            "dbo.Users",
            c => new
                {
                    Id = c.Guid(nullable: false, identity: true),
                    ProfilePicUrl = c.String(),
                    Name = c.String(),
                    LastName = c.String(),
                    Email = c.String(),
                    DateCreated = c.DateTimeOffset(nullable: false, precision: 7),
                    DateModified = c.DateTimeOffset(nullable: false, precision: 7),
                    Event_Id = c.Guid(),
                })
            .PrimaryKey(t => t.Id)
            .ForeignKey("dbo.Events", t => t.Event_Id)
            .Index(t => t.Event_Id);

    }

    public override void Down()
    {
        DropForeignKey("dbo.Events", "UserId", "dbo.Users");
        DropForeignKey("dbo.Comments", "EventId", "dbo.Events");
        DropForeignKey("dbo.Users", "Event_Id", "dbo.Events");
        DropForeignKey("dbo.Events", "User_Id1", "dbo.Users");
        DropForeignKey("dbo.Events", "User_Id", "dbo.Users");
        DropIndex("dbo.Users", new[] { "Event_Id" });
        DropIndex("dbo.Events", new[] { "User_Id1" });
        DropIndex("dbo.Events", new[] { "User_Id" });
        DropIndex("dbo.Events", new[] { "UserId" });
        DropIndex("dbo.Comments", new[] { "EventId" });
        DropTable("dbo.Users");
        DropTable("dbo.Events");
        DropTable("dbo.Comments");
    }
}

Если я удаляю код в EventConfig и помещаю его в OnModelCreating, это создает правильные отношения.

Моя процедура удаления старых сценариев миграции выглядит следующим образом:

  1. Удалить всю базу данных
  2. Удалить папку миграции в решении
  3. Включить-Миграции
  4. Add-Migration Initial
  5. Update-Database

Мой вопрос: мои старые сценарии миграции как-то применяются, а не новые, потому что эта схема не работает?отражать последние изменения в отношениях.

...