C# 实体框架CTP代码第一列命名和关系
我刚开始研究新的实体框架CTP5代码的第一位 我有以下非常简单的实体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
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”);
为什么特定用户有多个帐户?