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();