gpt4 book ai didi

c# - 使用具有导航属性的接口(interface)

转载 作者:可可西里 更新时间:2023-11-01 09:15:14 25 4
gpt4 key购买 nike

我正在尝试使用 Entity Framework 4、POCO 和 Code-Only 设置一个项目。

是否可以在 Entity Framework 中将导航属性的类型作为接口(interface)?

我有一个“任务”类。一个任务可以分配给一个用户或一个组,每个用户或组都由一个单独的类表示并存储在单独的表中。这些类看起来像这样:

public class User : IAssignable
{
public string Name { get; set; }
public int ID { get; set; }
public string Email { get; set; }
public string Password { get; set; }
}

public class Group : IAssignable
{
public string Name { get; set; }
public int ID { get; set; }
public string Manager { get; set; }
public string Department { get; set; }
}

public class Task
{
public string Title { get; set; }
public DateTime DueDate { get; set; }
public string Details { get; set; }
public IAssignable AssignedTo { get; set; }
}

有没有办法将 AssignedTo 属性作为 Entity Framework 中的导航属性?我假设 EF 必须有某种类型的鉴别器才能知道它是否需要查看用户表或组表,但我可以使用 Code-Only 或 EDMX 找出映射。

最佳答案

您可以在导航属性中使用接口(interface),看看这个解决方案,因为它与问题相同: How to use interface properties with CodeFirst

关于c# - 使用具有导航属性的接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2016847/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com