【发布时间】:2015-04-10 17:13:39
【问题描述】:
我得到了一个数据库,应该可以对它进行基本的 CRUD 操作。这可以通过使用 .NET 4.5/MVC5 和 EF6 快速完成。这意味着数据库优先方法。
新要求:(弹性)搜索。
为自定义类创建索引时(未链接到模型中的其他类),一切都很好。当我使用一个有很多外键的类时,事情就停止了。该数据库由 100 个表组成,具有 400 多个外键。
我认为问题可能是循环引用(客户有 n 个合同,其中有对客户的引用,有一个合同列表,......你明白了)。最终我得到了 OutOfMemory 异常,一切都崩溃了。
代码:
public static Uri node;
public static ConnectionSettings settings;
public static ElasticClient client;
public ActionResult TestIndex()
{
node = new Uri("http://localhost:9200");
settings = new ConnectionSettings(node, defaultIndex: "crudapp");
client = new ElasticClient(settings);
var indexSettings = new IndexSettings();
indexSettings.NumberOfReplicas = 1;
indexSettings.NumberOfShards = 1;
//The next line causes the OutOfMemoryException
client.CreateIndex(c => c.Index("crudapp")
.InitializeUsing(indexSettings)
.AddMapping<Customer>(map => map.MapFromAttributes(maxRecursion: 1)));
foreach (Customer c in db.Customer.Where(a => a.Active == true))
client.Index(c);
return View("Index");
}
如何告诉 Nest 停止递归或不使用某些对象?
示例类:
public partial class Customer
{
public Customer()
{
this.CustomerContract = new HashSet<CustomerContract>();
}
public int Customerid { get; set; }
public string CustomerName { get; set; }
public string Description { get; set; }
public bool Active { get; set; }
public virtual ICollection<CustomerContract> CustomerContract { get; set; }
}
public partial class CustomerContract
{
public CustomerContract()
{
this.Host = new HashSet<Host>();
}
public int CustomerContractid { get; set; }
public string CustomerContractName { get; set; }
public string Description { get; set; }
public int CustomerID { get; set; }
public bool Active { get; set; }
public virtual Customer Customer { get; set; }
public virtual ICollection<Host> Host { get; set; }
}
【问题讨论】:
标签: c# entity-framework elasticsearch asp.net-mvc-5 nest