I have some questions with relationship of ef code first.
My code:
public class user
{
public user()
{
}
public int id { get; set; }
public string code { get; set; }
public string name { get; set; }
}
public class dep
{
public int id { get; set; }
public Nullable<int> UserId { get; set; }
public virtual user User { get; set; }
}
modelBuilder.Entity<dep>()
.HasOptional(t => t.User)
.WithOptionalDependent();
The table dep that code first auto generated has a foreign key named User_Id,
but that is not what I want.
I want to use the column UserId that I defined in the model.
How will I change my code.
Think you can just add an attribute
on your User property.
Now, the question is why ? You’re not in a Database, but in a ORM, an object world. You should work with User Navigation property, not with UserId. And if you need UserId, you should use User.Id.
But… that’s your soultion 😉