Frage

Ich habe eine kurze Frage über die Zuordnungen zwischen [Order Details], [Products] und [Orders] in der Nordwind-datbase einrichten.

[Order Details] hat keinen Primärschlüssel, und sieht wie folgt aus

[Order Details]
OrderId (int)
ProductId (int)
...

Also meine Frage ist, wie kann ich (und ich) einrichten meine OrderDetails Klasse Arbeit wie das?

public class OrderDetails
{
    public virtual Order Order { get; set; }
    public virtual Product Product { get; set; }
    public Decimal UnitPrice { get; set; }
    public int Quantity { get; set; }
    public Decimal Discount { get; set; }
}

Mein Datenkontext wie folgt aussieht

public class NorthwindDb : DbContext
{
    public DbSet<Product> Products { get; set; }
    public DbSet<Category> Categories { get; set; }
    public DbSet<Order> Orders { get; set; }
    public DbSet<OrderDetails> OrderDetails { get; set; }
    public DbSet<Customer> Customers { get; set; }


    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Configurations.Add(new OrderDetailsConfiguration());
    }

    public static void InitializeBecauseOfThatWeirdMetaDataThingThatIDontUnderstandYet()
    {
        Database.SetInitializer<NorthwindDb>(null);
    }

}

Und mein OrderDetailsConfiguration (leer, weil ich nicht weiß, was ich tue)

public class OrderDetailsConfiguration : EntityConfiguration<OrderDetails>
{
    public OrderDetailsConfiguration()
    {
        //HasKey(x => x.Order.OrderId);
        //HasKey(x => x.Product.ProductId);
    }
}

Für Hinweise oder Ideen wäre toll.

War es hilfreich?

Lösung

First you would have to explicitly put in the PKs and FKs inside OrderDetails class:

public class OrderDetails 
{        
    public int OrderID { get; set; }
    public int ProductID { get; set; }

    public virtual Order Order { get; set; }
    public virtual Product Product { get; set; }

    public Decimal UnitPrice { get; set; }
    public int Quantity { get; set; }
    public Decimal Discount { get; set; }
}

Then with the following code you can specify the PKs for OrderDetailsConfiguration class:
(Please note that in Northwind database both OrderID and ProductID are PKs for OrderDetails table).

public class OrderDetailsConfiguration : EntityConfiguration<OrderDetails> 
{
    public OrderDetailsConfiguration() 
    {
        HasKey(od => new 
        {
            od.ProductID,
            od.OrderID
        });
    }
}

And also you can use RecreateDatabaseIfModelChanges strategy to force your database to be recreated every time you change the class model:

Database.SetInitializer<NorthwindDb>(
        new RecreateDatabaseIfModelChanges<NorthwindDb>());

The interesting point is that once you made these changes EF will automatically infer the FKs from OrderDetails class and create the relationships to Orders and Products table on OrderID and ProductID based on the Conventions for Code First:

Code First will infer that any property named <navigation property name><primary key property name> (i.e. ProductsProductID), <principal class name><primary key property name> (i.e. ProductProductID) or <primary key property name> (i.e. ProductID), with the same data type as the primary key, represents a foreign key for the relationship. If multiple matches are found then precedence is given in the order listed above. Foreign key detection will not be case sensitive.

Andere Tipps

Mark key fields with [Key] attribute. You will need to add [Column(Order = x)] in case of compound keys, x = 0, 1, 2, ... Numbering can start from any number https://msdn.microsoft.com/en-us/data/jj591583.aspx

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top