0

I'm trying to create a OneToMany relationship between 2 classes "User and Broker" where User has many Broker.

The problem is when I add a new Broker a new User has beeing created too and I don't know why it happens.

How could I fix it ?

User

public class User{


    public long id                  { get; set; }

    public String name              { get; set; }

    public IList<Broker> brokers    { get; set; }

Broker

public class Broker{

    public long id                          { get; set; }
    public String name                      { get; set; }   

    public User user                        { get; set; }

UserMap

public class UserMap : EntityTypeConfiguration<User>{
    public UserMap (){
        this.ToTable("Users");
        this.HasKey<long>(u => u.id);
        this.Property(u => u.id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

        this.Property(u => u.name).IsRequired().HasMaxLength(50);

        HasMany(u => u.brokers).WithRequired(c => c.user);

BrokerMap

public class BrokerMap : EntityTypeConfiguration<Broker>{
    public BrokerMap (){
        this.ToTable("Brokers");
        this.HasKey<long>(c => c.id);
        this.Property(c => c.id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

        this.Property(c => c.name).IsRequired().HasMaxLength(50);       

        HasRequired(u => u.user).WithMany(c => c.brokers);

Saving Data

Broker broker = new Broker();
User user = Session["User"] as User;
broker.user = user;

context.Brokers.Add(broker);
context.SaveChanges();
FernandoPaiva
  • 4,410
  • 13
  • 59
  • 118
  • You are adding a user to `broker.user`, so the user is inserted together with `borker` as expected. – Amir Molaei Mar 11 '19 at 19:39
  • @rad But I already have the User inserted and `broker.user = user` is getting the user and making relationship. Are there any way to do this ? – FernandoPaiva Mar 11 '19 at 19:45

2 Answers2

1

user is a new entity and totally disconnected from your dbcontext, therefore, it is inserted as a new entity together with the broker.
However to avoid this, first you need to read the user from database and then assign it to the broker so that without inserting a new user, the relationship is established.

  User user = Session["User"] as User;
  var userFromDb = context.Users.FirstOrDefault(t = > t.Id == user.Id);
  broker.user = userFromDb ;
  context.Brokers.Add(broker);
  context.SaveChanges();
Amir Molaei
  • 3,700
  • 1
  • 17
  • 20
0

This is the expected behavior of the Entity Framework, the facility they offer you. In other words, you don't have to worry about creating a user and once created relate it to that Broker.

German Gonzalez
  • 301
  • 1
  • 3
  • 10
  • It means that I don't need to use `broker.user = user` ? Then, how it will knows what User I want to create the relationship especially because the User already exists ? – FernandoPaiva Mar 11 '19 at 19:55
  • Sorry, I misunderstood the original question. I think this [link](https://stackoverflow.com/questions/48816929/prevent-adding-new-record-on-related-table-entity-in-entity-framework) could resolve your problem. – German Gonzalez Mar 11 '19 at 20:01