简体   繁体   English

实体框架一对一关系都需要

[英]Entity Framework one to one relationship both required

Hi I have a database to be setup as follows: 嗨,我有一个数据库要设置如下:

It has a User who belongs to an Area : 它有一个User属于一个Area

public class User : IEntity
    {
        public int UserId { get; set; }
        public string Username { get; set; }
        public int AreaId { get; set; }
        public string CreatedByUserName { get; set; }
        public DateTime CreatedDateTime { get; set; }
        public string LastModifiedByUserName { get; set; }
        public DateTime? LastModifiedDateTime { get; set; }

        //Navigation properties
        public virtual Area Area { get; set; }
    }

It has an Area which has a DefaultAdmin of type User : 它具有一个类型为UserDefaultAdminArea

    public class Area : IEntity
        {
            public virtual int Id { get; set; }
            public virtual string Name { get; set; }
            public virtual int DefaultAdminId { get; set; }
            public string CreatedByUserName { get; set; }
            public DateTime CreatedDateTime { get; set; }
            public string LastModifiedByUserName { get; set; }
            public DateTime? LastModifiedDateTime { get; set; }
    }


 // Navigation properties
        public virtual User DefaultAdmin { get; set; }
    }

Is it even possible to set something up like this? 甚至可以设置这样的东西吗? They both require each other so when starting with these tables empty you can never create one because the former requires the later. 它们都相互要求,所以当这些表开始为空时,您将永远无法创建表,因为前者需要后者。

Assuming the underlying table schemas have NOT NULL constraints on the foreign keys, this set of relationships would also be problematic from the database side of things (even if leaving EntityFramework out of the picture) 假设基础表模式对外键具有NOT NULL约束,那么从数据库的角度来看,这组关系也将是有问题的(即使将EntityFramework置于图片之外)

You will be able to do one of the following: 您将可以执行以下操作之一:

a) Reconsider the relationships between areas and users a)重新考虑区域与用户之间的关系

b) Or if the relationship described really is accurate: b)或所描述的关系确实是正确的:

  • temporarily relax the requirement such that a first user can be created without an area 暂时放宽要求,以便可以创建第一个没有区域的用户
  • create a first user (perhaps AdminUser) 创建第一个用户(也许是AdminUser)
  • create a first area with the first user as the DefaultAdmin 使用第一个用户作为DefaultAdmin创建一个第一个区域
  • associate the first user with the area created in the previous step 将第一个用户与上一步中创建的区域相关联
  • reinstate the relationship requirements as described in the question 恢复问题中所述的关系要求

The steps above could be done as part of a database initialization script run when deploying a database. 上面的步骤可以作为部署数据库时运行的数据库初始化脚本的一部分来完成。 Once a first area and user exist, you will be able to add more without breaking the requirements described. 一旦第一个区域和用户存在,您将可以添加更多内容而不会违反上述要求。

I should point out that the relationships are not really one-to-one as described in the title of the question as: 我应该指出,这种关系并不是真正的一对一关系,正如问题标题所述:

  • A user could be the DefaultAdmin for many areas (1 to many) 用户可以是许多区域(1到许多)的DefaultAdmin
  • An area can have many users (1 to many) 一个区域可以有很多用户(1到很多)

Hope that helps... 希望有帮助...

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM