[英]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
: 它具有一个类型为
User
的DefaultAdmin
的Area
:
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)或所描述的关系确实是正确的:
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: 我应该指出,这种关系并不是真正的一对一关系,正如问题标题所述:
Hope that helps... 希望有帮助...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.