【发布时间】:2014-11-20 16:18:52
【问题描述】:
两者
modelBuilder.Entity<Entity2>().HasOptional(e => e.Entity1).WithMany().HasForeignKey(e => e.Entity1Id);
和
modelBuilder.Entity<Entity2>().HasOptional(e => e.Entity1).WithOptionalDependent(e => e.Entity2);
在构建两个数据库表时似乎产生了相同的结果——那么它们之间的真正区别是什么,正确的用法是什么?
型号:
public class Entity1
{
public int Id { get; set; }
public virtual Entity2 Entity2 { get; set; }
}
public class Entity2
{
public int Id { get; set; }
public int? Entity1Id { get; set; }
public virtual Entity1 Entity1 { get; set; }
}
生成这些表:
Entity1
------------
PK Id
FK Entity2_Id (which I do not want it to create this FK anyway)
Entity2
------------
PK Id
FK Entity1d
【问题讨论】:
-
这与我的问题有关:stackoverflow.com/questions/27027895/… ...但不同之处在于我想了解这两种特定模型构建器方法之间的区别,因为它们似乎做同样的事情
标签: entity-framework