[英]Custom Relationship in Entity Framework
I have got the following three tables (just an example) 我有以下三个表(仅作为示例)
public class User
{
public int id { get; set; }
public string name { get; set; }
public List<Code> Codes { get; set; }
}
public class Device
{
public int id { get; set; }
public string name { get; set; }
public List<Code> Codes { get; set; }
}
public class Code
{
public int id { get; set; }
public int entity_id { get; set; }
public string entity_type { get; set; }
public string code { get; set; }
}
Now a code can either belong to a User or a Device, which will be determined by the value of entity_type (ie 'user' or 'device'). 现在,代码可以属于用户或设备,这将由entity_type的值(即“用户”或“设备”)确定。 How can this be achieved in Entity Framework ?
如何在实体框架中实现?
You could change your Code class to 您可以将您的代码类更改为
public class Code
{
public int id { get; set; }
public User user { get; set; }
public Device device { get; set; }
public string code { get; set; }
}
This way one of those properties can be null. 这样,这些属性之一可以为空。
Hope it helps. 希望能帮助到你。
The drawback is that you could have a code that have a user an a device 缺点是您可能拥有一个让用户和设备共享的代码
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.