Yai*_*vet 3 c# entity-framework entity-framework-4 ef-code-first
代码优先的流畅API 的HasOptional方法有一个DataAnnotation属性对应吗?
我想用属性标记我的属性,而不是使用流畅的API.
这是我当前的模型和OnModelCreating代码:
public class Employee
{
#region Properties
public int EmployeeID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int? ManagerID { get; set; }
public Employee Manager { get; set; }
#endregion
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Employee>().
HasOptional(e => e.Manager).
WithMany().
HasForeignKey(m => m.ManagerID);
}
Run Code Online (Sandbox Code Playgroud)
谢谢
不,这样的属性不存在(与[Required]属性相反).我认为原因是没有必要因为
[Required]属性来建立所需的关系.因此,似乎不需要这样的属性(除非可能在类定义中使可选关系显式化,但属性上方的注释也会这样做).