entity-framework - 使用虚拟属性更新类时, Entity Framework 验证失败

标签 entity-framework entity-framework-4.1 entity-framework-4.3

当类包含虚拟属性时,更新类的属性时遇到问题。这是我的代码

 public class Policy
            {
                [Key]
                [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
                public long id { get; set; }

                [UIHint("Company"), Required]
                public virtual Company company { get; set; }

                [UIHint("Productor"), Required]
                public virtual Productor productor { get; set; }

                [MaxLength(1000)]
                public string comments { get; set; }
            }

        public class Company
        {
            [Key]
            [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
            public long id { get; set; }

            [MaxLength(100)]
            public string name { get; set; }

        }

    //Then Productor class is the same as company but with another name

     public static int updateComment(long id, string comments)
            {
                MemberPolicies mp = new MemberPolicies();

                Policy p = mp.Policies.Single(o => o.id == id);
                p.comments = comments;

                int afectedRecords = -1;
                try
                {
                    afectedRecords = mp.SaveChanges();
                }
                catch (DbEntityValidationException dbEx)
                {
                    foreach (var validationErrors in dbEx.EntityValidationErrors)
                    {
                        foreach (var validationError in validationErrors.ValidationErrors)
                        {
                            Trace.TraceInformation("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);
                        }
                    }
                }
                return afectedRecords;
            }


导致验证错误的属性是company和company,但是我只想更新属性注释。

感谢您的帮助。

谢谢

最佳答案

当您尝试保存实体(该死)时,EF不会延迟加载您的虚拟属性。
您可以执行以下任一操作。

使用包括:

Policy p = mp.Policies
    .Include(p => p.company)
    .Include(p => p.productor).Single(o => o.id == id);
p.comments = comments;


或使用负载:

Policy p = mp.Policies.Single(o => o.id == id);
p.comments = comments;
mp.Entry(p).Reference(p => p.company).Load();
mp.Entry(p).Reference(p => p.productor).Load();


或者更好的是,您可以编写更优雅的代码,如here所述。

关于entity-framework - 使用虚拟属性更新类时, Entity Framework 验证失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11264046/

相关文章:

c# - 如何在一对多关系 EF 4.3.1 的导航集合中删除后正确清理

entity-framework - 如何使用 Entity Framework 在Sql Server Express中获取下一个序列号?

c# - 为什么我从存储过程中得到-1?

c# - Entity Framework : how to map multiple tables with different keys to one entity?

c# - 如何将 Entity Framework (代码优先)与动态创建的连接字符串和单文件数据库一起使用?

sql - 传递到字典中的模型项的类型为“System.Data.Entity.DynamicProxies.Object”

c# - Entity Framework 4.1 通过设置外键映射关系

entity-framework - 指定的表不存在。 [ __MigrationHistory ]

entity-framework - 在 Azure AppFabric 中缓存 Entity Framework 结果

entity-framework - 从 EF 生成动态 IQueryable<T>