2
我正在開發一個.NET Framework 4.6.2和Entity Framework 6.1.3的C#庫代碼首先在SQL Server 2012數據庫中使用它。重要角色的多重性必須是0..1
我有以下兩類:
public class Product
{
public int ProductId { get; set; }
// Omitted for brevity
public virtual ICollection<ProductionOrder> ProductionOrders { get; set; }
}
public class ProductionOrder
{
public int ProductionOrderId { get; set; }
public int? ProductId { get; set; }
// Omitted for brevety
public virtual Product Product { get; set; }
// Omitted for brevity
}
有了這兩個配置類:
class ProductionOrderConfiguration : EntityTypeConfiguration<ProductionOrder>
{
public ProductionOrderConfiguration()
{
HasKey(po => po.ProductionOrderId);
Property(c => c.ProductionOrderId)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
Property(po => po.ProductionOrderId)
.IsRequired();
Property(po => po.ProductId)
.IsOptional();
// Omitted for brevity
}
}
class ProductConfiguration : EntityTypeConfiguration<Product>
{
public ProductConfiguration()
{
HasKey(p => p.ProductId);
Property(p => p.ProductId)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
// Omitted for brevity
HasMany(p => p.ProductionOrders)
.WithRequired(po => po.Product)
.HasForeignKey(p => p.ProductId);
}
}
但是,當我嘗試運行它,我得到了以下信息,我不明白:
Product_ProductionOrders::多重性在角色中無效 'Product_ProductionOrders_Sour ce'關係 'Product_ProductionOrders'。因爲依賴角色中的所有屬性都可以爲空,所以主體角色的多重性必須爲 爲'0..1'。
我想代表其型號爲:
生產訂單可以有零或一個產品。一個產品可以在一個或n個生產訂單中。
我不知道如何將主體角色的多重性設置爲'0..1'。