簡體   English   中英

具有鏈接表的實體框架代碼優先遷移問題

[英]Entity Framework Code First Migration issue with linking tables

我首先使用EF 5.0代碼,並通過PM控制台進行自動遷移。

我試圖將第四次遷移應用於項目,並且鏈接表的基於約定的命名導致它們被刪除並使用不同的名稱重新創建。 在以前的任何遷移過程中都沒有發生這種情況。

范例:

我有2類用戶和站點。

InitialCreate遷移

按照慣例,這創建了一個稱為“ UserSites”的鏈接表。

CreateTable(
           "dbo.UserSites",
            c => new
                {
                    User_Id = c.Guid(nullable: false),
                    Site_Id = c.Guid(nullable: false),
                })
            .PrimaryKey(t => new { t.User_Id, t.Site_Id })
            .ForeignKey("dbo.Users", t => t.User_Id, cascadeDelete: true)
            .ForeignKey("dbo.Sites", t => t.Site_Id, cascadeDelete: true)
            .Index(t => t.User_Id)
            .Index(t => t.Site_Id);

一切正常。

跳到今天:

第四次遷移

這將刪除UserSites鏈接表並創建一個SiteUsers鏈接表。

顯然不理想!

public override void Up()
    {
        DropForeignKey("dbo.UserSites", "User_Id", "dbo.Users");
        DropForeignKey("dbo.UserSites", "Site_Id", "dbo.Sites");
        DropIndex("dbo.UserSites", new[] { "User_Id" });
        DropIndex("dbo.UserSites", new[] { "Site_Id" });


        CreateTable(
            "dbo.SiteUsers",
            c => new
                {
                    Site_Id = c.Guid(nullable: false),
                    User_Id = c.Guid(nullable: false),
                })
            .PrimaryKey(t => new { t.Site_Id, t.User_Id })
            .ForeignKey("dbo.Sites", t => t.Site_Id, cascadeDelete: true)
            .ForeignKey("dbo.Users", t => t.User_Id, cascadeDelete: true)
            .Index(t => t.Site_Id)
            .Index(t => t.User_Id);

        DropTable("dbo.UserSites");

我很茫然地解釋這一點。

自從第一次實現以來,這兩個類都沒有發生過變化……我想如果應用此方法,將會遭受數據丟失的困擾。

所以對這個問題:

  1. 我可以只從遷移腳本中刪除此代碼,然后繼續使用現有結構嗎?

  2. 是否有我不知道的陷阱可能導致了這個問題?

任何/所有幫助非常感謝!

編輯:

我僅按如下方式定義了類,並允許按照約定構建模型。 我更改了dbcontext的名稱空間,僅此而已! 使我困惑。

public class User
{
   public virtual ICollection<Site> Sites { get; set; }
}

public class Site
{
    public virtual ICollection<User> Users { get; set; }
}

這是一個奇怪的情況。 您是否在DbContext任何更改? 如何在類中聲明SitesUsers屬性?

我認為您應該向數據庫上下文顯式添加多對多關系,如下所示:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);

    modelBuilder.Entity<User>()
                .HasMany<Sites>(u => u.Sites)
                .WithMany(e => e.Users)
                .Map(
                    m =>
                        {
                            m.MapLeftKey("User_Id");
                            m.MapRightKey("Site_Id");
                            m.ToTable("UserSites");
                        });

    //for prevent error 'The referential relationship will result in a cyclical reference that is not allowed'
    modelBuilder.Entity<Sites>()
                .HasRequired(s => s.User)
                .WithMany()
                .WillCascadeOnDelete(false);
}

然后刪除您的第4個遷移,然后嘗試再次添加

如果刪除遷移代碼,則會捕獲導航錯誤,例如外鍵問題,錯誤的表名等。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM