我正在使用.NET 4.8。我有 3 個表:TableA、TableB 和 TableC。TableA 已經設定了與 TableB 和 TableC 的外鍵關系。這是使用代碼優先遷移完成的。
這是 TableA 的代碼:
public class TableA : Entity<int>
{
public bool SomeColumn1 { get; set; }
public bool SomeColumn2 { get; set; }
public bool SomeColumn3 { get; set; }
public virtual TableB TableB { get; set; }
public virtual TableC TableC { get; set; }
}
因此,在我的資料庫 TableA 中,這些列TableB_Id與TableC_Id它們各自的表相關。
我想向 TableA 添加一個唯一索引,以便TableB_Id和的組合TableC_Id是唯一的。
我想DatabaseContext.cs使用 FLUENT 模式在我的檔案中執行此操作。我DatabaseContext.cs在OnModelCreating()方法中添加了這個:
modelBuilder.Entity<TableA>().HasIndex(a => new { a.TableB, a.TableC }).IsUnique();
(a.TableB并且a.TableC是我進入時唯一可用的選項a.)
然后我編譯我的解決方案并嘗試創建一個遷移,但我收到一條錯誤訊息
序列不包含匹配元素
我發現這篇 SO 帖子建議在基本物體中添加值,所以我將 TableA 的代碼更改為:
public class TableA : Entity<int>
{
public bool SomeColumn1 { get; set; }
public bool SomeColumn2 { get; set; }
public bool SomeColumn3 { get; set; }
[Required]
[Index("IX_TableBAndTableC", 1, IsUnique = true)]
public int TableB_Id { get; set; }
public virtual TableB TableB { get; set; }
[Required]
public int TableC_Id { get; set; }
[Index("IX_TableBAndTableC", 2, IsUnique = true)]
public virtual TableC TableC { get; set; }
}
并且TableB_Id現在TableC_Id可以在我的代碼中使用,DatabaseContext.cs但是當我編譯解決方案并創建遷移時,它會洗掉 TableA 上的現有列TableB_Id和TableC_Id列,并將它們替換為TableB_Id1然后TableC_Id1創建索引。
然后我發現這篇 SO 帖子建議向基本物體添加外鍵裝飾器,因此我注釋掉了DatabaseContext.cs檔案中的代碼并將我的 TableA 物體中的代碼更改為:
public class TableA : Entity<int>
{
public bool SomeColumn1 { get; set; }
public bool SomeColumn2 { get; set; }
public bool SomeColumn3 { get; set; }
[ForeignKey("TableB_Id")]
public virtual TableB TableB { get; set; }
[ForeignKey("TableC_Id")]
public virtual TableC TableC { get; set; }
}
but when I compile the solution and generate a migration the migration is empty with nothing in the up() and down() methods.
I searched in the official documentation but was not able to find anything that specifically instructed on how to create a multi column unique index using the default columns that get created when I set up the foreign keys using a code first migration (TableA.TableB_Id and TableA.TableC_Id). The columns TableA.TableB_Id and TableA.TableC_Id are created by the entity framework automatically so I am not able to use them directly in my DatabaseContext.cs file.
Please can someone give me some help in figuring out how to create a multi column unique index on these two default foreign keys TableB_Id and TableC_Id by either adding something to the DatabaseContext.cs file or the base entity code for TableA, or both.
A correct, clearly explained answer will be marked as accepted and upvoted. Thanks.
uj5u.com熱心網友回復:
在玩弄它之后,我發現這個頁面討論了物體框架中的外鍵關系。在這里,我找到了一種不同的方式來為我的TableA物體格式化我的代碼,這將完成我需要做的事情。
所以這是我的TableA物體的代碼:
public class TableA : Entity<int>
{
public bool SomeColumn1 { get; set; }
public bool SomeColumn2 { get; set; }
public bool SomeColumn3 { get; set; }
[ForeignKey("TableB")]
public int TableB_Id { get; set; }
public virtual TableB TableB { get; set; }
[ForeignKey("TableC")]
public int TableC_Id { get; set; }
public virtual TableC TableC { get; set; }
}
這與我在原始帖子中嘗試的一種方法非常相似,我只是有一些設定不正確。
在我將此代碼添加到我的TableA物體后,回到我的DatabaseCOntext.cs檔案中,我能夠在OnModelCreating()方法中添加這一行:
modelBuilder.Entity<TableA>().HasIndex(a => new { a.TableB_Id, a.TableC_Id }).IsUnique();
現在指向欄位TableA.TableB_Id并且TableA.TableC_Id以前不可用。
我編譯了我的解決方案并創建了一個新的遷移,它似乎可以完成我需要它做的所有事情。
我再次編譯了解決方案并運行了遷移和列TableA.TableB_Id,TableA.TableC_Id并且仍然存在,設定為它們各自表的外鍵,并且對它們有一個唯一約束,不允許插入重復的行。
我希望這對其他人有幫助。如果我所做的不清楚,請發表評論,我會盡力澄清。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/441675.html
標籤:c# sql-server entity-framework foreign-keys entity-framework-migrations
上一篇:如何通過asp.netAPI控制器獲取所有表名的串列
下一篇:從同一個API獲取兩種型別的資料
