级联插入多个表-最佳实践

时间:2018-08-06 10:05:12

标签: c# entity-framework linq

我有3个表(国家/地区,州和城市),在创建客户端时会使用客户端ID填充副本表

默认表

tbl_Country
public int Id { get; set; }
public string Name { get; set; }

tbl_State
public int Id { get; set; }
public int CountryId { get; set; }
public string Name { get; set; }

tbl_City
public int Id { get; set; }
public int StateId { get; set; }
public string Name { get; set; }

客户表

tbl_Client_Country
public int Id { get; set; }
public int ClientId { get; set; }
public string Name { get; set; }

tbl_Client_State
public int Id { get; set; }
public int CountryId { get; set; }
public int ClientId { get; set; }
public string Name { get; set; }

tbl_Client_City
public int Id { get; set; }
public int StateId { get; set; }
public int ClientId { get; set; }
public string Name { get; set; }

插入脚本应该以使用以下内容开始循环:

var countries = from x in _context.tbl_Country select x;

喜欢:

foreach(var country in countries){

//insert country and return key for state insert

//select all state using country.Id

 foreach(var state in states){

 //insert state and return key for city insert

 //select all city using state.Id

 foreach(var city in cities){

 }


 }

}

我看过带有相关标题的问题,但是这种情况有所不同。

问题:是否有一种更高效的方法而无需级联。

0 个答案:

没有答案