EF Code First 将多个类映射到一个表

发布于 2024-11-26 13:38:11 字数 1386 浏览 5 评论 0原文

我有一个相当复杂的数据库,我想首先将其映射到代码,但我遇到一个需要将其分成两个单独的类的表的问题。

表结构的基本示例如下:

Site
----
Id
Name

Person
------
Id
Name
Dob
Address
Rank
Age
SiteId

PersonRoleLink
--------------
PersonId
RoleId

Role
----
Id
Name

基本上,一个站点有很多人,每个人可以有很多角色。

根据人员所扮演的角色,取决于填充的属性。

理想情况下,我想要一个如下的类结构:

public class Site {
  public int Id { get; set; }
  public string Name { get; set; }

  public virtual ICollection<Customer> Customers { get; set; }
  public virtual ICollection<Employee> Employees { get; set; }
  public virtual ICollection<Manager> Managers { get; set; }
}

public class PersonBase {
  public int Id { get; set; }
  public string Name { get; set; }

  public virtual Site Site { get; set; }
}

public class Customer : PersonBase {
  public string Name { get; set; }
  public DateTime Dob { get; set; }
}

public class Employee: PersonBase {
  public int Age { get; set; }
}

public class Manager: PersonBase {
  public int Rank { get; set; }
}

其中 CustomerEmployeeManager 都来自 Person 表并依赖于 PersonRoleLink 表中的相关链接。

因此,当我调用诸如 Site.Managers 之类的内容时,返回的 Person 表中的行是具有正确 SiteId 且具有指向 Role 链接的行> 称为“Manager”

这种结构首先可以在代码中实现吗?如果是这样,如何实现此映射?如果不是,该结构是否有任何可行的替代方案?

谢谢

I've got quite a complex database that I want to map to code first but I am having a problem with a table that needs to be split out into two separate classes.

A basic example of the tables structure is as follows:

Site
----
Id
Name

Person
------
Id
Name
Dob
Address
Rank
Age
SiteId

PersonRoleLink
--------------
PersonId
RoleId

Role
----
Id
Name

Basically a Site has many people and each person can have many roles.

Depending on the role the person has depends what properties are populated.

Ideally I would like a class structure as follows:

public class Site {
  public int Id { get; set; }
  public string Name { get; set; }

  public virtual ICollection<Customer> Customers { get; set; }
  public virtual ICollection<Employee> Employees { get; set; }
  public virtual ICollection<Manager> Managers { get; set; }
}

public class PersonBase {
  public int Id { get; set; }
  public string Name { get; set; }

  public virtual Site Site { get; set; }
}

public class Customer : PersonBase {
  public string Name { get; set; }
  public DateTime Dob { get; set; }
}

public class Employee: PersonBase {
  public int Age { get; set; }
}

public class Manager: PersonBase {
  public int Rank { get; set; }
}

Where Customer, Employee and Manager all come from the Person table and are dependant on the relevant link in the PersonRoleLink table.

So when I call something like Site.Managers the rows in the Person table returned are the ones with the correct SiteId and with a link to a Role called 'Manager'

Is this structure possible in code first? If so how is the mapping for this achieved and if not are there any viable alternatives to this structure?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

说不完的你爱 2024-12-03 13:38:11

对于 EF 来说这是不可能的,因为它是数据驱动的映射,并且 EF 代码首先不支持它,除了一个例外:每个层次结构的表继承映射,其中映射表中的单个列的值可以与映射的子实体不同。在您的情况下,与实体类型不同的值是两个关系。此外,人员表中的一条记录可以代表多个实体实例。这是另一个问题,因为这些实例没有唯一密钥,而 EF 将无法做到这一点。

一般来说,您假设的对象模型是错误的,因为您可以有一个人既是员工又是经理。它不应该产生两个不同的“Person”对象。直接使用Person 的导航属性来访问角色,并创建帮助方法来获取经理、员工和客户。

It is not possible with EF because it is data driven mapping and EF code first doesn't support it except single exception: Table per hierarchy inheritance mapping where value of single column in mapped table can differ mapped sub entities. In your case the value differing the type of entity is two relations away. Moreover one record from person table can represent multiple entity instances. That is another problem because these instances will not have unique key and EF will not be able to do that.

Generally your supposed object model is wrong because you can have single person who is employee and manager. It should not result in two different "Person" objects. User Person directly with navigation property to roles and create helper methods to get Managers, Employees and Customers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文