The multiplicity of the main role must be equal to 0..1

I am developing a C # library with .NET Framework 4.6.2 and Entity Framework 6.1.3. Code First for use in a SQL Server 2012 database.

I have two classes:

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
}

      

With these two configuration classes:

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);
    }
}

      

But when I try to run it I get the following message, which I don't understand:

Product_ProductionOrders :: multiplicity is not valid on the 'Product_ProductionOrders_Source' role in the 'Product_ProductionOrders' relationship. Because all properties in the Dependent Role are zeroed out, the multiplicity of the main role must be '0..1'.

The model I'm trying to imagine is:

A production order can have zero or one product. And the product can be in one or n production orders.

I don't know how to set the multiplicity of the main role to "0..1".

+3


source to share


1 answer


Well, this is just a matter of different terminology used in the fluent API. Display:

multiplicity 1     => Required
multiplicity 0..1  => Optional

      

According to your model, you need to change



.WithRequired(po => po.Product)

      

to

.WithOptional(po => po.Product)

      

+8


source







All Articles