Reputation: 2883
I am using entity framework 4.1 and code-first mapping.
public class Account
{
public Int AccountId {get;set};
public string Name {get;set};
public int? ProfileId {get;set;}
public virtual Profile {get;set;}
}
public class Profile
{
public int ProfileId {get;set;}
public DateTime Created {get;set;}
public virtual Account {get;set;} // navigation back to account
}
public AccountMapper()
{
ToTable("..")
HasKey(x => x.AccountId);
HasOptional(x => x.Profile).WithRequired(x => x.Account) // invalid column exception
// Try HasOptional(x => x.Profile).WithRequired(x => x.Account).Map(x => x.MapKey("ProfileId")) // rror 0019: Each property name in a type must be unique. Property name 'ProfileId' was already defined.
}
public ProfileMappeR()
{
ToTable("..")
HasKey(x => x.ProfileId);
}
Well, the question is pretty simple: where are you doing mistake?
Thanks, Martin.
Upvotes: 1
Views: 123
Reputation: 2380
One to one relationships are a little special with code first. Here is a good blog arcticle about it:
Cheers
Upvotes: 1