实体框架播种复杂对象?

时间:2016-07-21 16:34:42

标签: entity-framework

我正在试图弄清楚如何为复杂的实体框架代码种子化第一个对象。目前我的代码允许我插入简单的对象,但必须有一种方法可以做更复杂的项目,即带有AdressType字段的地址。

context.AddressTypes.AddOrUpdate(
         p => p.Name,
         new AddressType { Name = "Original" },
         new AddressType { Name = "Shipping" },
         new AddressType { Name = "Billing" }
        );
context.Addresses.AddOrUpdate(
         a => a.Address1,
         new Address
          {
          Address1 = "1234 West Main",
          City = "Hannibal",
          Region = "MO",
          PostalCode = "12345",
          Country = "USA",
          Type = context.AddressTypes.Where(a=>a.Name=="Original")
          });

但我可以"发现"我不能通过id添加一个" Where"名字等于。查找可以工作,除了我不能保证每个种子的id将是什么,并希望以名称为基础。

有关如何执行此操作的任何建议? TIA

1 个答案:

答案 0 :(得分:0)

解决方案: 向种子类添加了参考System.Linq。然后能够使用where子句。

相关问题