简体   繁体   English

具有两个指向彼此的表的实体框架注释

[英]Entity Framework Annotation with two tables pointing at each other

I am trying to use CodeFirst to generate my database. 我正在尝试使用CodeFirst生成数据库。

I have two tables Staff and Team , There is one Team Leader in each team that is a foreign key to a staffID, and each staff is associated to one Team. 我有两个表StaffTeam ,每个团队中都有一个Team Leader,这是一个StaffID的外键,并且每个员工都与一个Team相关联。

public class Staff
{
    [Key]
    public int ID { get; set; }
    public string Name { get; set; }
    public string Username { get; set; }
    public string PasswordHash { get; set; }
    public string Salt { get; set; }
    public string Token { get; set; }
    public string Mobile { get; set; }
    public string Email { get; set; }
    public bool Admin { get; set; }
    public bool Active { get; set; }

    public int TeamID { get; set; }

    [ForeignKey("TeamID")]
    public Team Team { get; set; }
}

public class Team
{
    [Key]
    public int ID { get; set; }
    public string Name { get; set; }
    public int TeamLeaderID { get; set; }

    [ForeignKey("TeamLeaderID")]
    public Staff TeamLeader { get; set; }
}

Because each one is pointing to the other I get an error Unable to determine the principal end of an association between the types 'Team' and 'Staff'. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations. 因为每个人都指向另一个,所以我得到一个错误, Unable to determine the principal end of an association between the types 'Team' and 'Staff'. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations. Unable to determine the principal end of an association between the types 'Team' and 'Staff'. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations. How do I annotate it in such a way that it understands why I am doing this. 我如何以一种能理解我为什么这样做的方式对其进行注释。

Think about it this way... what comes first the team or the team leader? 这样想吧……团队或团队负责人首先要做什么? If you try to create a team leader you can't because you have to first specify a team! 如果您尝试创建团队负责人,那么您将无法做到,因为您必须先指定一个团队! But if you want to create a team you can't because you must specify who the team leader is according to your foreign key constraints. 但是,如果您要创建团队,则不能这样做,因为必须根据外键约束来指定团队负责人。

You will have to ease up in some way and either make it so that a team can have an optional team leader, or a staff member can optionally belong to a team. 您将必须以某种方式放松自己,或者做到这一点,以便团队可以有一个可选的团队负责人,或者一个工作人员可以有选择地属于一个团队。

You do this by changing one of the foreign key IDs to a nullable type: 您可以通过将外键ID之一更改为可为空的类型来实现:

public int? TeamLeaderID { get; set; }

Your code seems like you are trying to include business rule enforcement/responsibility via referential integrity. 您的代码似乎正在尝试通过引用完整性包括业务规则实施/职责。 You have a one to many relationship your Team -> Staff. 您的团队->员工之间存在一对多的关系。 You just add a boolean for the TeamLeader. 您只需为TeamLeader添加一个布尔值。 Your logic before doing a database write should check to see if you have an existing TeamLeader or not already. 在执行数据库写操作之前,您的逻辑应该检查以查看您是否已有TeamLeader。

public class Staff
{
    [Key]
    public int ID { get; set; }
    public string Name { get; set; }
    public string Username { get; set; }
    public string PasswordHash { get; set; }
    public string Salt { get; set; }
    public string Token { get; set; }
    public string Mobile { get; set; }
    public string Email { get; set; }
    public bool Admin { get; set; }
    public bool Active { get; set; }
    public IsTeamLeader { get; set; }

}

public class Team
{
    [Key]
    public int ID { get; set; }
    public string Name { get; set; }

    //virtual keyword tells Code First to create the proper Foreign Key relationship 
    public virtual List<Staff> Members{ get; set; }
}

If you had a large system, with many developers, you could use the fluent API to accomplish your goal and enforce your team leader rule at the database level, thus preventing an out of touch developer from inadvertently adding a second team leader to any give team but if this is a small to normal size project, with small teams that are aware of the basics of the company/project than a simple one to many relationship will accomplish the mission and you can rely on your business rules/logic to enforce/protect the database data so that there is one team leader for any given team at any give time. 如果您的大型系统拥有很多开发人员,则可以使用流畅的API来实现您的目标并在数据库级别执行团队负责人规则,从而防止失调的开发人员无意中将第二个团队负责人添加到任何团队中但是,如果这是一个小规模到正常规模的项目,并且拥有了解公司/项目基本知识的小型团队,而不是简单的一对多关系就可以完成任务,那么您可以依靠业务规则/逻辑来实施/保护数据库数据,以便在任何给定时间任何给定团队都有一位团队负责人。 Consider an AddUpdateTeamMember type method that is called by everyone that enforces the team leader requirement. 考虑一个执行团队负责人要求的所有人都调用的AddUpdateTeamMember类型方法。 A stored procedure is another great way to easily solve this problem if you are still considering a model first approach and still experimenting with code first. 如果仍在考虑模型优先方法并且仍在先进行代码试验,则存储过程是轻松解决此问题的另一种好方法。

Consider too, what if there was a need for two team leaders on one team at some future point of time in the project, database might get too inconveniently "locked down" to scale to this change. 还要考虑一下,如果在项目的某个将来的某个时间点需要一个团队中的两个团队负责人,数据库可能会很不方便地“锁定”以适应这种变化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在实体框架上具有彼此键的两个表 - Two tables that have keys to each other on Entity Framework 两个表在Entity Framework 6 Code First中相互引用 - Two tables referencing each other in Entity Framework 6 Code First 实体框架6表具有许多外键,每个外键指向不同的表 - Entity Framework 6 table with many foreign keys each pointing to different tables 3个表之间的相互关系的实体框架映射 - Entity Framework mapping of 3 tables with relationship between each other 添加两个实体且FK相互指向 - Adding two entities with FKs pointing to each other 连接两个表的实体框架 - Joining two tables entity framework 在实体框架中联接两个表 - joining two tables in entity framework 如何在实体框架中连接两个或多个表并在数据网格视图中显示每个表中的选定列 - How to join two or more tables in entity framework and display selected columns from each tables in data grid view 实体框架-表需要两个指向同一表的外键 - Entity Framework - table needs two foreign keys pointing at same table Entity Framework DB 前两个上下文指向同一个数据库模式 - Entity Framework DB First two contexts pointing to the same database schema
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM