繁体   English   中英

EF未为虚拟属性创建表

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM