Entity Framework 6 添加外键

Entity Framework 6 Adding foreign key

我有模型

public class Order
{
    [Key]
    public int ID { get; set;}

    public string Name { get; set; }
}

public class Consumer
{
    public virtual List<Order> Orders { get; set;}
}

我遇到的问题是当您创建一个 Consumer 对象并引用现有的 Order 时,有没有办法只传递订单 ID 并允许 entity framework 完成其余的工作.

我可以让它工作的唯一方法如下:

//Look up order
var order = dbContext.Order.Where(x=>x.ID == orderID)
var consumer = new Consumer { new List{ order} };
dbContext.Consumer.Add(consumer);

有没有办法不用查订单就可以做到这一点?仅插入订单密钥的示例?像这样:

var consumer = new Consumer {
    new List { 
       new Order { ID = orderID } 
    } 
};
dbContext.Consumer.Add(consumer);

注意:最大的不同是我只传递 "orderID" 而不必实际传递整个 Order 对象。

更新:

public class Order
{
    [Key]
    public int ID { get; set;}

    public string Name { get; set; }

    public virtual List<Order> InheritedOrders{ get; set;}
}

DbContext 中:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
    modelBuilder.Entity<EntityClass>().HasMany(x=>x.InheritedOrders).WithMany().Map(cs =>
            {
                cs.MapLeftKey("ParentID");
                cs.MapRightKey("ChildID");
                cs.ToTable("OderInheritance");
            });
}

在这种情况下,由于您引用了相同类型的对象并且您使用了自动生成的 WithMany 生成的 table,它会如何工作?

谢谢, D

您需要在订单模型中有一个参考码。像这样:

public class Order
{
    [Key]
    public int ID { get; set;}

    public string Name { get; set; }

    public int ConsumerId { get; set; }

    public virtual Consumer Consumer { get; set; }
}

public class Consumer
{
    public Consumer()
    {
        this.Orders = new HashSet<Order>();
    }

    public virtual List<Order> Orders { get; set;}
}

然后就可以添加没有订单的Consumer了。

var consumer = new Consumer { };
dbContext.Consumer.Add(consumer);

稍后添加订单时,您可以像这样附加消费者...

var order = new Order {
    Id = 1,
    Name = "Order 1",
    ConsumerId = 1
};
dbContext.Order.Add(order);

然后像这样查询回来...

foreach (Order order in Consumer.Orders)
{
    // do something with each order.
}

更新

对于您的上述问题,我假设您的意思是如何找到子订单?如果您使用 Fabio 提供的代码,您可以像这样获取记录...

foreach (var order in Consumer.Orders.ChildOrders)
{
    // Do something with the child orders
}

或嵌套

foreach (var order in Consumer.Orders)
{
    // parent order

    foreach (var childOrder in order.ChildOrders)
    {
        // child orders of parent.
    }
}

你可以这样建立自我关系 (1:n):

public class Order
{
    [Key]
    public int ID { get; set;}

    public string Name { get; set; }

    public int? ParentOrderId{ get; set; }

    public virtual Order ParentOrder { get; set; }

    public virtual ICollection<Order> ChildOrders { get; set; }
}

映射:

modelBuilder.Entity<Order>()
    .HasOptional(i => i.ParentOrder)
    .WithMany(i => i.ChildOrders)
    .HasForeignKey(i => i.ParentOrderId)
    .WillCascadeOnDelete(false);

插入订单(如@sgtrice1 所说):

var order = new Order 
{
    Name = "Order 1",
    ParentOrderId = 1 // FK HERE, IT WILL MAKE THE RELATIONSHIP
};
dbContext.Order.Add(order);
dbContext.SaveChanges();