Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 实体框架CTP代码第一列命名和关系_C#_Entity Framework_Code First_Ef Code First - Fatal编程技术网

C# 实体框架CTP代码第一列命名和关系

C# 实体框架CTP代码第一列命名和关系,c#,entity-framework,code-first,ef-code-first,C#,Entity Framework,Code First,Ef Code First,我刚开始研究新的实体框架CTP5代码的第一位 我有以下非常简单的实体 User int Id; Account Account; Account int Id; User AccountOwner; ICollection<User> Users; 用户 int-Id; 账户; 账户 int-Id; 用户帐户所有者; i收集用户; 因此,帐户有一个所有者(用户),可以有多个用户。这些关系被转换为数据库中的帐户表和用户表 在用户表中,我得到两个collumn,Accou

我刚开始研究新的实体框架CTP5代码的第一位

我有以下非常简单的实体

User
 int Id;
 Account Account;

Account
 int Id;
 User AccountOwner;
 ICollection<User> Users;
用户
int-Id;
账户;
账户
int-Id;
用户帐户所有者;
i收集用户;
因此,帐户有一个所有者(用户),可以有多个用户。这些关系被转换为数据库中的
帐户
表和
用户


在用户表中,我得到两个collumn,
AccountId
AccountId1
,是否有任何方法可以控制这些列的生成名称。另外,我实际上希望在
Accounts
表中的
AccountOwner
属性中有一个
UserId
列,但似乎该引用位于
Users
表中。如果所有的键都没有
AccountId…n
,那就太好了:)

这里发生了一些事情

如果用户和帐户之间的关系为1:1,则选择您不想要的那一方。如果您不告诉它,它就无法知道该放在哪一边,因此您可以使用fluent API或属性来告诉它。您可能希望将一侧设置为可选,另一侧设置为必需,这将迫使FK移动到另一侧,例如

User
...
[Optional]
Account Account;

Account
...
[Required]
User AccountOwner;
或者,您可以使用fluent API:

config.Entity<User>().HasOptional(u => u.Account).WithRequired(a => a.AccountOwner)
config.Entity<User>().Property(u => u.Id).HasColumnName("Id");
或者再次使用fluent API:

config.Entity<User>().HasOptional(u => u.Account).WithRequired(a => a.AccountOwner)
config.Entity<User>().Property(u => u.Id).HasColumnName("Id");
config.Entity().Property(u=>u.Id).HasColumnName(“Id”);

为什么特定用户有多个帐户?