【发布时间】:2014-02-26 15:10:07
【问题描述】:
我有以下类(我只显示重要的属性):
public class TypeLocation
{
[Key]
public int Id {get; set;}
public string Country {get; set;}
public string State {get; set;}
public string County {get; set;}
public string City {get; set;}
public List<Offer> Offers {get; set; }
public TypeLocation()
{
this.Offers = new List<Offer>();
}
}
public class Offer
{
public int Id { get; set; }
public ICollection<TypeLocation> LocationsToPublish { get; set; }
}
这会在数据库中创建以下内容:
我的问题/问题
TypeLocations 表预先填充了国家/州/县/城市记录的静态列表,其中一个或多个可以与 LocationsToPublish 属性中的优惠相关联。当我尝试使用以下代码添加位置时,Entity Framework 在TypeLocation 表中添加一条新记录,然后通过在OfferTypeLocations 表中添加一条记录来建立关联。
public static bool AddPublishLocation(int id, List<TypeLocation> locations)
{
try
{
using (AppDbContext db = new AppDbContext())
{
Offer Offer = db.Offers
.Include("LocationsToPublish")
.Where(u => u.Id == id)
.FirstOrDefault<Offer>();
//Add new locations
foreach (TypeLocation loc in locations)
{
Offer.LocationsToPublish.Add(loc);
}
db.SaveChanges();
}
return true;
}
catch
{
return false;
}
}
我不希望将新记录添加到 TypeLocations 表中,而只是在 OfferTypeLocations 表中创建关联的关系记录。对我做错了什么有任何想法吗?
解决方案
感谢@Mick 在下面的回答,我找到了解决方案。
public static bool AddPublishLocation(int id, List<TypeLocation> locations)
{
try
{
using (AppDbContext db = new AppDbContext())
{
Offer Offer = db.Offers
.Include("LocationsToPublish")
.Where(u => u.Id == id)
.FirstOrDefault<Offer>();
//Add new locations
foreach (TypeLocation loc in locations)
{
//SOLUTION
TypeLocation ExistingLoc = db.AppLocations.Where(l => l.Id == loc.Id).FirstOrDefault<TypeLocation>();
Offer.LocationsToPublish.Add(loc);
}
db.SaveChanges();
}
return true;
}
catch
{
return false;
}
}
发生的情况是,使用现有的 AppDbContext,我从 TypeLocations 表中检索现有记录(此处标识为 AppLocations),然后将其添加到 LocationsToPublish 实体中。
关键是我要使用当前的 AppDbContext(用 Using() 语句包装)来完成所有工作。此上下文之外的任何数据都是纯粹的信息性数据,用于协助在 AppDbContext 上下文中发生的记录查找或创建。我希望这是有道理的。
【问题讨论】:
-
如果您打开分析器,您会看到此解决方案会为添加的每个位置生成额外的查询。它会起作用,但是它会比下面的解决方案慢很多。
-
List
实际上是从网页上发布的数据生成的,因此没有任何上下文可以传入。实际上,我们不应该在任何时间点传入太多的 TypeLocation ,最多 10-15 个。
标签: c# entity-framework