gpt4 book ai didi

entity-framework - 在 Entity Framework 4 中选择和更新多对多

转载 作者:行者123 更新时间:2023-12-04 07:25:19 25 4
gpt4 key购买 nike

enter image description here

我对 EF 比较陌生,并且上面有以下实体模型,其中包含一个 Assets 和一个国家/地区。 Assets 可以属于多个国家,因此与国家具有多对多关系(在数据库中有一个连接表,其中两个字段都作为主键)。

我希望能够做到以下几点:

首先,当我从模型中检索 Assets (或 Assets )时,我想获取与其关联的各个国家/地区。然后我希望能够将国家列表绑定(bind)到 IEnumerable。以这种方式检索国家为我提供了国家对象的 EntityCollection,它具有 ToList() 的扩展方法。因此,不确定我是否会走上正确的道路。这是我的 GetAll 方法:

public IEnumerable<Asset> GetAll()
{
using (var context = CreateAssetContext())
{
var assetEntities = context.Assets.Include("Countries").ToList();
return AssetMapper.FromEntityObjects(assetEntities);
}
}

其次,我希望能够选择 AssetId == 某些值的国家/地区列表。

最后,我希望能够更新给定 Assets 的国家/地区列表。

非常感谢。

最佳答案

Firstly when I retrieve an asset (or assets) from the model I want to get the respective countries that its associated with. I would then like to be able to bind the countries list to an IEnumerable.



不确定我是否理解正确,但是 EntityCollection<T>实现 IEnumerable<T> ,所以你不需要做任何特别的事情,你可以使用 Asset.Countries加载包括国家/地区在内的 Assets 后。

Secondly I want to be able to select a list of countries where the AssetId == some value.


using (var context = CreateAssetContext())
{
var countries = context.Countries
.Where(c => c.Assets.Any(a => a.AssetId == givenAssetId))
.ToList();
}

或者:
using (var context = CreateAssetContext())
{
var countries = context.Assets
.Where(a => a.AssetId == givenAssetId)
.Select(a => a.Countries)
.SingleOrDefault();
}

第二个选项是可以的(从 SQL 的角度不确定它是否比第一个更好)因为 AssetId是主键,所以只能有一个 Assets 。用于按其他条件查询 - 例如 Asset.Name == "XYZ" - 你可以期待不止一种 Assets ,我更喜欢第一种选择。第二次你必须替换 Select通过 SelectManySingleOrDefault通过 ToList并使用 Distinct过滤掉可能的重复国家。 SQL 可能会更复杂。

Finally I want to be able to update the list of countries for a given Asset.



这更加棘手,因为您需要处理以下情况:1)已将国家/地区添加到 Assets 中,2)已从 Assets 中删除国家/地区,3)已与 Assets 相关的国家/地区。

假设您有一个国家 ID 列表 ( IEnumerable<int> countryIds),并且您希望将这些国家与给定 Assets 相关联:
using (var context = CreateAssetContext())
{
var asset = context.Assets.Include("Countries")
.Where(a => a.AssetId == givenAssetId)
.SingleOrDefault();

if (asset != null)
{
foreach (var country in asset.Countries.ToList())
{
// Check if existing country is one of the countries in id list:
if (!countryIds.Contains(country.Id))
{
// Relationship to Country has been deleted
// Remove from asset's country collection
asset.Countries.Remove(country);
}
}
foreach (var id in countryIds)
{
// Check if country with id is already assigned to asset:
if (!asset.Countries.Any(c => c.CountryId == id))
{
// No:
// Then create "stub" object with id and attach to context
var country = new Country { CountryId = id };
context.Countries.Attach(country);
// Then add to the asset's country collection
asset.Countries.Add(country);
}
// Yes: Do nothing
}
context.SaveChanges();
}
}

编辑

对于第二次往返数据库的价格,您可能可以使用以下更简单的代码:
using (var context = CreateAssetContext())
{
var asset = context.Assets.Include("Countries")
.Where(a => a.AssetId == givenAssetId)
.SingleOrDefault();

if (asset != null)
{
// second DB roundtrip
var countries = context.Countries
.Where(c => countryIds.Contains(c.CountryId))
.ToList();

asset.Countries = countries;

context.SaveChanges();
}
}

EF 的变更检测应识别已从 Assets 的国家/地区列表中添加或删除了哪些国家/地区。我不是 100% 确定后一个代码是否能正常工作。

关于entity-framework - 在 Entity Framework 4 中选择和更新多对多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7780109/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com