Question

I have two tables in my database which have a one-to-one relationship. I want to access them with EF using the code first approach. I have written the POCO classes like this:

public class User
{
    public int Id { get; set; }
    public string Login { get; set; }
    public string Password { get; set; }
    public string Email { get; set; }

    public virtual Profile Profile { get; set; }
}

public class Profile
{
    public int UserId { get; set; }
    public string FullName { get; set; }
    public DateTime? Birthday { get; set; }
    public bool? Male { get; set; }
}

And also I described the relationships between that classes in the OnModelCreating as explained here. So that's how looks:

modelBuilder.Entity<Profile>().HasKey(x => x.UserId);
modelBuilder.Entity<User>().
    HasOptional(u => u.Profile).
    WithRequired();

But when I run the application I'm getting this exception when trying to access the User tables:

Invalid column name "User_Id"

Although when I let EF create a new database on its own with these two tables, it creates a database that has the same structure as mine and it works.

I was using EF 4.3 (I tried 4.2 too) and MS SQL 2008. What am I doing wrong? I can't believe that it's a bug of Entity Framework.

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top