1

I am using the entity framework (code first).

I would like to know if I really need to use a property with Id for relationship with another entity as in the code below.

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

public class Profile
{
    public int Id { get; set; }
    public string Description{ get; set; }
}

For this way when I insert a user by setting the profileid property performs perfectly.

But when I don't use the profileid property in the Profile class,

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

public class Profile
{
    public int Id { get; set; }
    public string Description{ get; set; }
}

the execution the insert method adds another profile record. Why?

My mapping:

public class EntityMapping<Entity> : EntityTypeConfiguration<Entity> where Entity : EntityBase
{
    public EntityMapping()
    {
        HasKey(e => e.Id);
        Property(e => e.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
    }
}

public class UserMapping : EntityMapping<User>
{
    public UserMapping() : base()
    {
         ToTable("USER");
         Property(p => p.Id).HasColumnName("USER_CD_USER");
         Property(p => p.Login).HasColumnName("USER_TX_LOGIN").HasMaxLength(10).IsRequired();
         Property(p => p.Password).HasColumnName("USUA_TX_PASSWORD").HasMaxLength(8).IsRequired();
         HasRequired(e => e.Profile).WithMany(p => p.Users).Map(p => p.MapKey("PROF_CD_PROFILE"));
     }
}

public class ProfilelMapping : EntityMapping<Profile>
{
    public ProfileMapping()
        : base()
    {
        ToTable("PROFILE");
        Property(p => p.Id).HasColumnName("PROF_CD_PROFILE");
        Property(p => p.Description).HasColumnName("PROFILE_DS_PROFILE").HasMaxLength(20).IsRequired();
        HasMany(e => e.Users).WithRequired(p => p.Profile);
    }
}
Mark Hall
  • 51,582
  • 8
  • 87
  • 106
Vinicius Saeta
  • 183
  • 2
  • 12

2 Answers2

1

You are asking two questions.

Do I need to use FK property?

No you don't but EF behavior changes if you use it or not. More about it is in separate answer and linked blog article.

Why EF inserts Profile again?

Creating relations with existing entities requires special care. EF doesn't check if your entity exists in the database - you must tell it to EF. Here is one of many ways how to achieve that (without loading profile from the database):

var user = GetNewUserSomewhere();
context.Users.Add(user);

// Dummy profile representing existing one.
var profile = new Profile() { Id = 1 };
// Informing context about existing profile.
context.Profiles.Attach(profile);

// Creating relation between new user and existing profile
user.Profile = profile;

context.SaveChanges();
Community
  • 1
  • 1
Ladislav Mrnka
  • 349,807
  • 56
  • 643
  • 654
-1

Short answer: Yes. It's the way EF work. It needs to store the foreign key in a dedicated property. Have you ever generated the class structure from a database? It always adds that key property. There are cases you don't need the Profile property loaded, but later you might want to retrieve it. That's what the dedicated ProfileId property is used, it will read the key value from there and load the object.

Tomislav Markovski
  • 11,818
  • 4
  • 43
  • 69
  • [EF doesn't need](http://stackoverflow.com/questions/5281974/code-first-independent-associations-vs-foreign-key-associations/5282275#5282275) to store FK in dedicated property. – Ladislav Mrnka Dec 26 '11 at 12:41