C# 标识关系配置不会在数据库中生成标识规范

C# 标识关系配置不会在数据库中生成标识规范,c#,entity-framework,entity-framework-5,C#,Entity Framework,Entity Framework 5,好的,我使用此配置在OrderPage及其子OrderPageShipDate实例集合之间设置标识关系: public class OrderPageShipDateConfiguration : EntityTypeConfiguration<OrderPageShipDate> { public OrderPageShipDateConfiguration() { this.HasKey(t => new { t.OrderPageId, t

好的,我使用此配置在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);
    }
公共类OrderPageShipDateConfiguration:EntityTypeConfiguration { public OrderPageShipDateConfiguration() { this.HasKey(t=>new{t.OrderPageId,t.Id}); 属性(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; }
}
//
///此类表示在特定时间从特定公告添加到订单的一组项目。
/// 
公共类订单页:IEntity,IAuditInfo
{       
#区域建设
//...
#端区
公共int Id{get;set;}
//其他属性。。。
公共虚拟ICollection发货日期{get;set;}
}
_

//
///此类表示已添加到订单的订单页面的发货日期。
/// 
公共类OrderPageShipDate:IEntity
{
#区域建设
// ...
#端区
公共int Id{get;set;}
public int OrderPageId{get;set;}
公共虚拟OrderPage OrderPage{get;set;}
公共日期时间ShipDate{get;set;}
}
我没有在OrderPageShipDate表的Id列上设置标识规范

知道为什么吗

这可能与这些类实现的IEntity接口中指定的Id属性有关吗


正如您在这里看到的,我已经尝试了数据注释方法和Fluent API方法。

按照惯例,EF应该将Id作为键,并在其上放置identity属性。我认为这个.HasKey(t=>new{t.OrderPageId,t.Id})可能是这个问题,您想在这里使用复合键吗?

对于当前的问题,即数据库中没有创建标识规范,我必须向Collin致谢。他让我走上正确的道路来删除数据库表。因为我在开发中,我只是删除了整个数据库,让它自己重新创建。我使用的是database迁移,我没有意识到它不会进行那样的架构更改。

我认为这可能是罪魁祸首。请注意,您需要删除并重新创建此表,以使其更改列,EF不会在迁移中为您构建这样的框架。嗨,伙计们,我正在使用复合键来设置一个标识关系根据EF文件,并在此处举例说明:
/// <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; }
}