gpt4 book ai didi

c# - 通过字符串更改属性的状态

转载 作者:太空宇宙 更新时间:2023-11-03 15:28:11 25 4
gpt4 key购买 nike

我试图为我的 Entity Framework 更新方法提供属性名称列表,这些属性名称应标记为未更改。但是它确实获得了属性,但是在尝试更改状态时却出现以下错误:

The entity type RuntimePropertyInfo is not part of the model for the current context.

这是有道理的,但有没有办法获取当前上下文中的对象属性?

这是我的代码:

if (propertiesToSkip != null)
{
foreach (var propertyName in propertiesToSkip)
{
var prop = entity.GetType().GetProperty(propertyName);
_context.Entry(prop).State = EntityState.Unchanged;
}
}

希望大家帮帮忙。

编辑

public static void MigrateIqrAccountDetails(ref IUnitOfWork uow, Account account,
IEnumerable<List<string>> iqrContacts)
{
if (account.IqrAccount == null) throw new ArgumentException("Cannot migrate Account");

// Addresses
var addresses = ContactMigrationHelper.MigrateIqrContactAddresses(contact);
if (addresses != null)
account.Addresses.AddRange(addresses.Except(account.Addresses, new Comparers.AddressComparer()));
uow.Repository<IqrAccount>().Update(account.IqrAccount);
uow.Repository<Account>().Update(account, new List<string> {"Name"});
}

这是编辑对象的方法,应该在上下文中更新它。

帐户是这样收集的:

var accountToUpdate =
uow.Repository<Account>()
.GetAll(a => a.IqrAccount != null, "PhoneNumbers", "EmailAddresses", "Addresses")
.OrderBy(a => a.IqrAccount.UpdatedAt)
.FirstOrDefault();

在看起来像这样的存储库中:

public IEnumerable<T> GetAll(Func<T, bool> predicate = null, params string[] includes)
{
var query = Include(_objectSet, includes);
return predicate != null ? query.Where(predicate).Where(o => !o.IsRemoved) : query.Where(o => !o.IsRemoved);
}

protected virtual IQueryable<T> Include(IQueryable<T> query, params string[] includes)
{
if (includes == null) return query;

query = includes.Aggregate(query, (current, item) => current.Include(item));
return query;
}

我得到的错误是:

The INSERT statement conflicted with the FOREIGN KEY constraint "FK_dbo.Addresses_dbo.Accounts_AccountId". The conflict occurred in database "Test", table "dbo.Accounts", column 'Id'. The statement has been terminated.

调用 savechanges 时。

还有我的模型:

    public class Account : BaseEntity
{
public int Id { get; set; }
[Required]
public string Name { get; set; }

public virtual List<Address> Addresses { get; set; }

public int? CommentCollectionId { get; set; }

public virtual LabelCollection LabelCollection { get; set; }

public virtual User AccountManager { get; set; }

[ForeignKey("ParentAccount")]
public virtual int? ParentAccountId { get; set; }
public virtual Account ParentAccount { get; set; }
public virtual ICollection<Account> SubAccounts { get; set; }
}


public class Address : BaseEntity
{
public int Id { get; set; }
public string Country { get; set; }
public string Region { get; set; }
public string City { get; set; }
public string Street { get; set; }
public string Number { get; set; }
public string NumberExtension { get; set; }
public string ZipCode { get; set; }
public bool IsActive { get; set; }
public string Premise { get; set; }
public string SubPremise { get; set; }
public Location Coordinates { get; set; }

[ForeignKey("Account")]
public int AccountId { get; set; }
public Account Account { get; set; }

}

我猜问题是帐户作为单独的对象参数传递,因此它不再在上下文中?这样做的好方法是什么?

最佳答案

好吧,我现在通过从 Address 实体中删除 Account 外键解决了这个问题,因为该关系也在 Account 实体中描述。虽然它现在确实有效,但我仍然不完全理解为什么当实体上存在外键时它不起作用。如果有人知道答案,我仍然想知道。

关于c# - 通过字符串更改属性的状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34510084/

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