如何在 Entity Framework 中指定外键列表?
How to specify a list of foreign keys in Entity Framework?
如何使用数据注释或 fluentapi 指定模型中的字符串列表,如外键列表?我知道我可以在列表中指定用户模型,但我想要一个字符串列表。
模型示例:
public class Allocation
{
[Key]
public int Id { get; set; }
...
public List<string> Users { get; set; }
}
public class User
{
[Key]
public string Id { get; set; }
...
}
即使是专家开发人员也会惨败 Entity Framework,所以我会告诉你一个小秘密。 写下您希望拥有的代码。
关于您的场景,您不必要地使事情复杂化了。让 Entity Framework 完成它的工作并为您处理关系!
建立这种关系模型所需要做的就是...
public class Allocation
{
public int Id { get; set; }
public virtual ICollection<User> Users { get; set; }
}
public class User
{
public int Id { get; set; }
public virtual Allocation Allocation { get; set; }
}
现在请注意,我没有写出我希望得到的代码,但那是因为我希望得到的代码对于这个问题来说太过分了,而且 high-level.但是,如果您确实想深入研究这个主题并找出 Entity Framework 真正可以为您做些什么,我会从这里开始...
https://lostechies.com/jimmybogard/2010/02/04/strengthening-your-domain-a-primer/
如何使用数据注释或 fluentapi 指定模型中的字符串列表,如外键列表?我知道我可以在列表中指定用户模型,但我想要一个字符串列表。
模型示例:
public class Allocation
{
[Key]
public int Id { get; set; }
...
public List<string> Users { get; set; }
}
public class User
{
[Key]
public string Id { get; set; }
...
}
即使是专家开发人员也会惨败 Entity Framework,所以我会告诉你一个小秘密。 写下您希望拥有的代码。
关于您的场景,您不必要地使事情复杂化了。让 Entity Framework 完成它的工作并为您处理关系!
建立这种关系模型所需要做的就是...
public class Allocation
{
public int Id { get; set; }
public virtual ICollection<User> Users { get; set; }
}
public class User
{
public int Id { get; set; }
public virtual Allocation Allocation { get; set; }
}
现在请注意,我没有写出我希望得到的代码,但那是因为我希望得到的代码对于这个问题来说太过分了,而且 high-level.但是,如果您确实想深入研究这个主题并找出 Entity Framework 真正可以为您做些什么,我会从这里开始...
https://lostechies.com/jimmybogard/2010/02/04/strengthening-your-domain-a-primer/