I just started looking at the new Entity Framework CTP5 code first bits.
I have the following very simple entities.
User
int Id;
Account Account;
Account
int Id;
User AccountOwner;
ICollection<User> Users;
So, accounts have an owner(User) and can have many users. These relationships is translated to an Accounts
table and an Users
table int the DB.
In the Users table I get two collumns, AccountId
and AccountId1
, is there any way to control the generated names for those columns. Also I would ac开发者_如何学运维tually expect there to be a UserId
column for the AccountOwner
property on the Accounts
table, but it seems that the reference is on the Users
table instead. It would be really nice NOT to the have AccountId...n
for all the keys :)
There's a couple of things going on here.
In the case of User and Account having 1:1 relationships with each other it's choosing the side you don't want. There's no way for it to know which side to put it on without you telling it so you can tell it either with the fluent API or with an attribute. It's likely you want to make one side optional and one required which will force the FK to the other side, e.g.
User
...
[Optional]
Account Account;
Account
...
[Required]
User AccountOwner;
Alternatively you can use the fluent API:
config.Entity<User>().HasOptional(u => u.Account).WithRequired(a => a.AccountOwner)
If you follow intellisense on that you can also use the constraint property to wire up the FK properties too.
To rename the FK properties if they're still not what you like either use the Column attribute, e.g.
User
[Column(Name="Id")]
int Id;
Or again with the fluent API:
config.Entity<User>().Property(u => u.Id).HasColumnName("Id");
精彩评论