Mennion
Mennion

Reputation: 2883

Entity framework 4.1 code-first mapping issue

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

Answers (1)

LueTm
LueTm

Reputation: 2380

One to one relationships are a little special with code first. Here is a good blog arcticle about it:

http://weblogs.asp.net/manavi/archive/2011/05/01/associations-in-ef-4-1-code-first-part-5-one-to-one-foreign-key-associations.aspx

Cheers

Upvotes: 1

Related Questions