![](/img/trans.png)
[英]Creating SQLite Virtual Table with EF context.Database.EnsureCreated?
[英]EF not creating table for virtual properties
我在代碼優先中使用EF。 我的模型包含兩個虛擬屬性,例如:
ClassA
public int Id { get; set; }
public string Name { get; set; }
public virtual List<int> Teams { get; set; }
public virtual List<int> Levels { get; set; }
EF為我創建了數據庫表,只有ClassA表。 沒有可以存儲團隊和級別數據的表。 當我運行Add-Migration命令時,如果EF認為沒有掛起的更改,則創建的遷移文件為空。 另外,當我運行Update-Database命令時,EF表示沒有掛起的更改。
我在這里想念什么?
為什么不這樣做:
public class Team {
public int Id { get; set; }
public virtual ICollection<Player> Players { get; set; }
}
public class Level {
public int Id { get; set; }
public virtual ICollection<Player> Players { get; set; }
}
public class Player {
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Team> Teams { get; set; }
public virtual ICollection<Level> Levels { get; set; }
}
如果您真的想要“輕巧”(請不要),可以這樣做:
public class Player {
public int Id { get; set; }
public string Name { get; set; }
//use comma separated values (starting and ending with ,)
public string Teams { get; set; }
public string Levels { get; set; }
}
假設您在上下文中擁有這些
var player1 = new Player(){Teams = ",1,2,"}
var player2 = new Player(){Teams = ",3,2,"}
現在,您可以同時加入第2團隊
var players = context.Players.Where(p=>p.Teams.Contains(",2,"));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.