![](/img/trans.png)
[英]Entity Framework (Code First) - How to clone an entity without foreign keys
[英]Code First - Entity Framework - How to expose foreign keys
我有以下数据对象:
public class Customer : System.Data.Entity.ModelConfiguration.EntityTypeConfiguration<Customer>
{
public Customer ()
{
// this.HasRequired(a => a.EyeColorType).WithMany().HasForeignKey(a => a.EyeColorTypeID);
}
[Key]
public int ID { get; set; }
[Required]
[MaxLength(100)]
public string FirstName { get; set; }
[Required]
[MaxLength(100)]
public string LastName { get; set; }
[Required]
[ForeignKey("EyeColorTypeID")]
public EyeColorType EyeColorType { get; set; }
// public int EyeColorTypeID { get; set; }
}
}
我想要做的是,在生成的Customer属性上公开EyeColorTypeID
字段,并在DB中指定名称。
如果我删除这两个注释,它可以工作,但我想为此使用数据注释。 像[ExposeForeignKey("EyeColorTypeID")]
,让它全部自动发生。
这可能吗? 我该怎么办呢?
只需将其添加到模型中并使导航属性为虚拟(对于延迟加载):
public class Customer : //etc
{
//your stuff
[Required]
public int EyeColorTypeID {get; set;}
public virtual EyeColorType EyeColorType { get; set; }
}
EF的命名约定将EyeColorTypeID
绑定到EyeColorType
导航属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.