【发布时间】:2015-07-28 05:10:09
【问题描述】:
我正在使用 Entity Framework 6.X 和代码优先方法。
我的要求是我有桌子
- 用户
- 产品
在输入产品时,我需要创建者名称、修改器名称来跟踪,这两个应该引用用户表。因为那些输入/修改的人应该在一个用户表中。
我的模型是:
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; }
}
但执行此操作时,它会生成 Db 而不会出错,并在 Product 的 CreatedBy、ModifiedBy 列上创建外部列。但是在产品中,它会创建 User_UserId、User_UserId1 作为额外的列。
我是实体框架的新手。
请帮帮我。
更新
当我将引用更改为一列时,只说 CreatedBy 那个额外的列不会出现在产品中。但是,当我如上所示再引用一列时,问题就来了。
【问题讨论】:
-
尝试添加
[InverseProperty("UserId ")]。看到这个问题stackoverflow.com/questions/15483019/… -
@NikolaiSamteladze 感谢您的回复。我需要在哪里添加这个属性?
标签: c# sql entity-framework entity-framework-6