Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6979305
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:52:10+00:00 2026-05-27T17:52:10+00:00

I am using the entity framework (code first). I would like to know if

  • 0

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);
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T17:52:11+00:00Added an answer on May 27, 2026 at 5:52 pm

    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();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to use Entity Framework Code first approach with SQLCE4 database. Everything
Using Entity Framework 4 and code first how would I create a model that
I have been using Entity Framework CTP with Code-First as in this tutorial by
I'm using code-first in entity-framework 4, Entity Framework Feature CTP 3 Is it possible
I'm using ASP.NET MVC 3 with the Entity Framework 4 code first approach and
I am using Entity Framework 4.1 code first with ASP.NET MVC 3 and Razor
Is there a way in Entity Framework 4 (using CTP4 and Code First if
I am using Entity Framework 4.1 code first and ASP.NET MVC 3 and I
Using the Entity Framework Code First paradigm I have defined the following objects and
I am using Entity Frameworks Code First. I have one entity that I need

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.