好的,我有这个配置在OrderPage和它的子OrderPageShipDate实例集合之间设置识别关系:
public class OrderPageShipDateConfiguration : EntityTypeConfiguration<OrderPageShipDate>
{
public OrderPageShipDateConfiguration()
{
this.HasKey(t => new { t.OrderPageId, t.Id });
this.Property(p => p.OrderPageId).HasColumnOrder(1);
this.Property(p => p.Id).HasColumnOrder(2)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
this.HasRequired(opi => opi.OrderPage)
.WithMany(op => op.ShipDates)
.HasForeignKey(opi => opi.OrderPageId);
}
与这些参与者:
/// <summary>
/// This class represents a a set of items added to an order from a particular bulletin at a particular time.
/// </summary>
public class OrderPage : IEntity, IAuditInfo
{
#region Construction
//...
#endregion
public int Id { get; set; }
// other properties...
public virtual ICollection<OrderPageShipDate> ShipDates { get; set; }
}
_
/// <summary>
/// This class represents a shipping date for an order page that has been added to an order.
/// </summary>
public class OrderPageShipDate : IEntity
{
#region Construction
// ...
#endregion
public int Id { get; set; }
public int OrderPageId { get; set; }
public virtual OrderPage OrderPage { get; set; }
public DateTime ShipDate { get; set; }
}
我没有在OrderPageShipDate表的Id列上设置标识规范。
知道为什么吗?
它是否与这些类实现的IEntity接口中指定的Id属性有关?
我已经尝试了Data Annotation方法和Fluent API方法,如您所见。
答案 0 :(得分:1)
按照惯例,EF应该选择Id作为密钥,并在其上添加identity属性。我想这个.HasKey(t =&gt; new {t.OrderPageId,t.Id})可能是问题,你想要一个复合键吗?
答案 1 :(得分:0)
对于在数据库中没有创建身份规范的直接问题,我必须给予Collin信用。他让我走上正确的道路来放弃数据库表。自从我开发以来,我只是放弃了整个数据库并让它重新创建。我正在使用数据库迁移,但我没有意识到它不会像那样进行模式更改。