简体   繁体   English

EF未为虚拟属性创建表

[英]EF not creating table for virtual properties

I'm using EF with code-first. 我在代码优先中使用EF。 My model contains two virtual properties like: 我的模型包含两个虚拟属性,例如:

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 has created db tables for me there is only ClassA table. EF为我创建了数据库表,只有ClassA表。 There is no table I can store Teams and Levels data. 没有可以存储团队和级别数据的表。 When I run Add-Migration command, created migration file is empty is EF thinks there is no pending changes. 当我运行Add-Migration命令时,如果EF认为没有挂起的更改,则创建的迁移文件为空。 Also when I run Update-Database command EF says there is no pending changes. 另外,当我运行Update-Database命令时,EF表示没有挂起的更改。

What am I missing here? 我在这里想念什么?

Why not do: 为什么不这样做:

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; }
}

In case you really want 'light weight' (please don't) you could do: 如果您真的想要“轻巧”(请不要),可以这样做:

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; }
}

Say you have these in your context 假设您在上下文中拥有这些

var player1 = new Player(){Teams = ",1,2,"}
var player2 = new Player(){Teams = ",3,2,"}

Now, you could get both in team 2 by 现在,您可以同时加入第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