小编Ade*_*Tas的帖子

EF7如何处理嵌套实体的更新操作

我试图找出更新实体及其所有子实体时的最佳实践.例如; 我有一个"雇主"更新服务,它将更新雇主实体和雇主的"地址"实体以及每个"地址"的"电话"实体.用户可以向现有雇主添加新地址,或者他们可以更新当前地址,或者他们可以删除一些地址,同样适用于每个地址的电话.你能帮我写出处理这种情况的理想代码吗?

我正在使用EF7 rc1,我使用Automapper将Dto映射到我的服务中的实体.

public partial class Employer
{
   public int EmployerId { get; set; }
   public int Name { get; set; }

   [InverseProperty("Employer")]
   public virtual ICollection<Address> Address { get; set; }
}

public partial class Address
{
   public int AddressId { get; set; }
   public int Address1{ get; set; }
   public int City { get; set; }

   [ForeignKey("EmployerId")]
   [InverseProperty("Address")]
   public virtual Employer Employer { get; set; }

   [InverseProperty("Address")]
   public virtual ICollection<Phone> Phone { get; set; }
}

public partial …
Run Code Online (Sandbox Code Playgroud)

c# entity-framework entity-framework-core asp.net-core

6
推荐指数
1
解决办法
510
查看次数

在实体属性中使用 NotMapped 属性时,EF7 添加迁移错误

我无法在实体属性上使用 [NotMapped] 属性,因为迁移添加命令会引发以下错误;

这是我的 POCO;

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace Domain.Entities
{
    public partial class Employer
    {
        public int EmployerId { get; set; }
        public bool? Blacklisted { get; set; }
        [NotMapped]
        public int TestProperty { get; set; }
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我收到的异常消息。

System.ArgumentNullException: Value cannot be null.
Parameter name: navigation
   at Microsoft.Data.Entity.Utilities.Check.NotNull[T](T value, String parameterName)
   at Microsoft.Data.Entity.Metadata.Conventions.Internal.ConventionDispatcher.OnNavigationAdded(InternalRelationshipBuilder relationshipBuilder, Navigation navigation)
   at Microsoft.Data.Entity.Metadata.Internal.InternalRelationshipBuilder.Relationship(InternalEntityTypeBuilder principalEntityTypeBuilder, InternalEntityTypeBuilder dependentEntityTypeBuilder, String navigationToPrincipalName, String navigationToDependentName, IReadOnlyList`1 dependentProperties, IReadOnlyList`1 principalProperties, Nullable`1 isUnique, Nullable`1 …
Run Code Online (Sandbox Code Playgroud)

c# entity-framework entity-framework-core asp.net-core entity-framework-migrations

5
推荐指数
1
解决办法
1887
查看次数