shanmugharaj
shanmugharaj

Reputation: 3914

Entity Framework producing two extra columns

I am using Entity Framework 6.X and a code-first approach.

My requirement is I have tables

  1. User
  2. Product

While entering product I need Creator Name, Modifier Name to keep track and these two should reference User table. Because those who entered/modified should be in a user table.

My model is:

public class User : AuditableEntity
{
    [Key]
    [StringLength(35)]
    public string UserId { get; set; }

    [Required]
    [Column(TypeName = "varchar")]
    public string Name { get; set; }

    public string Address { get; set; }

    [Required]
    public long MobileNo { get; set; }

    [Required]
    public string Password { get; set; }

    public string EmailId { get; set; }

    public string MiscData { get; set; }

    [Required]
    public Role Role { get; set; }

    public virtual ICollection<Product> Products { get; set; }
    public virtual ICollection<Product> Products1 { get; set; }
}

public class Product : AuditableEntity
{
    [Key]
    [StringLength(30)]
    public string Id { get; set; }

    [Required]
    public string ProductName { get; set; }

    public string Description { get; set; }

    [Required]
    [Column(TypeName = "Money")]
    public decimal Amount { get; set; }

    [Required]
    [StringLength(35)]
    public override string CreatedBy
    {
        get
        {
            return base.CreatedBy;
        }
        set
        {
            base.CreatedBy = value;
        }
    }

    [Required]
    [StringLength(35)]
    public override string ModifiedBy
    {
        get
        {
            return base.ModifiedBy;
        }
        set
        {
            base.ModifiedBy = value;
        }
    }

    [ForeignKey("CreatedBy")]
    public User User { get; set; }

    [ForeignKey("ModifiedBy")]
    public User User1 { get; set; }
}

But when executing this it generates Db without error and it creates foreign column on CreatedBy, ModifiedBy columns in Product. However in the products it creates User_UserId, User_UserId1 as extra columns.

I am new to Entity Framework.

Please help me.

UPDATE

When i changed the referencing to one column say only CreatedBy that extra column is not coming in products. But when i reference one more column as shown above the problem is coming.

Upvotes: 1

Views: 108

Answers (1)

Nikolai Samteladze
Nikolai Samteladze

Reputation: 7797

This didn't solve the problem and is left only for historical information. Please see the UPDATE for the solution.

See this SO question. Try adding [InverseProperty("UserId ")] like this:

[InverseProperty("UserId ")]
[ForeignKey("CreatedBy")]
public User User { get; set; }

[InverseProperty("UserId ")]
[ForeignKey("ModifiedBy")]
public User User1 { get; set; }

UDPATE

Also, EF might be confused where to map your User.Products and User.Products1. See this SO question and this MSDN article on code-first relationships. Try changing it to:

[InverseProperty("User")]
public virtual ICollection<Product> Products { get; set; }

[InverseProperty("User1")]
public virtual ICollection<Product> Products1 { get; set; }

Upvotes: 2

Related Questions