【问题标题】:Adding additional properties to an entity framework 4 code first CTP 5 entity向实体框架添加附加属性 4 代码优先 CTP 5 实体
【发布时间】:2011-07-06 13:34:18
【问题描述】:

我正在使用ASP.NET MVC 3Entity Framework code first CTP 5。我想知道是否可以添加未映射到表列的其他属性?

我有一个 News 类,它是这样定义的:

public class News : Entity
{
   public int NewsId { get; set; }
   public string Title { get; set; }
   public string Body { get; set; }
   public bool Active { get; set; }
}

我的数据库上下文类:

public class MyContext : DbContext
{
   public DbSet<News> Newses { get; set; }
}

在实体类中,我定义了如下属性:

public IList<RuleViolation> RuleViolations { get; set; }

我还没有对这部分进行编码,但我希望在验证对象时将所有损坏的规则添加到此列表中。我得到的错误是:

One or more validation errors were detected during model generation:

    System.Data.Edm.EdmEntityType: : EntityType 'RuleViolation' has no key defined. Define the key for this EntityType.
    System.Data.Edm.EdmEntitySet: EntityType: The EntitySet RuleViolations is based on type RuleViolation that has no keys defined.

这是我的存储库代码:

public News FindById(int newsId)
{
   return context.Database.SqlQuery<News>("News_FindById @NewsId",
      new SqlParameter("NewsId", newsId)).FirstOrDefault();
}

2011-03-02 更新:

这是我的Entity 课程:

public class Entity
{
   public IList<RuleViolation> RuleViolations { get; set; }

   public bool Validate()
   {
      // Still needs to be coded
      bool isValid = true;

      return isValid;
   }
}

这是我的RuleViolation 课程:

public class RuleViolation
{
   public RuleViolation(string parameterName, string errorMessage)
   {
      ParameterName = parameterName;
      ErrorMessage = errorMessage;
   }

   public string ParameterName { get; set; }
   public string ErrorMessage { get; set; }
}

这是我的上下文类:

public class MyContext : DbContext
{
   public DbSet<News> Newses { get; set; }

   protected override void OnModelCreating(ModelBuilder modelBuilder)
   {
      modelBuilder.Entity<News>().Ignore(n => n.RuleViolations);
   }
}

【问题讨论】:

    标签: asp.net asp.net-mvc entity-framework entity-framework-4 asp.net-mvc-3


    【解决方案1】:

    您可以通过在 MyContext 类的 OnModelCreating 方法中添加忽略规则来使用 Fluent API 忽略类型

    public class MyContext : DbContext {
    
      public DbSet<News> Newses { get; set; }
    
      protected override void OnModelCreating(ModelBuilder builder) {
    
        builder.Ignore<RuleViolation>()
    
      }
    
    }
    

    或者您可以使用NotMapped 属性忽略该属性

    public class Enitity {
    
      [NotMapped]
      public IList<RuleViolation> RuleViolations { get; set; }
    
      //other properties here
    
    }
    

    然后实体框架将忽略该属性。

    【讨论】:

    • @David:感谢您的扩展回答。我需要包含 base.OnModelCreating(modelBuilder); 吗?如果是这样,那么我是否需要在这段代码之后或之前包含您的代码?
    • @David:我仍然遇到同样的错误。我已经更新了我原来的帖子,请你看看。
    • 改用builder.Ignore&lt;RuleViolation&gt;();builder.Entity&lt;Entity&gt;().Ignore(x =&gt; x.RuleViolations);
    • @David:我试过 modelBuilder.Ignore();现在可以了。不确定这是否是正确的方法?我更喜欢忽略,但显然这是一个错误。
    • 您的错误是因为 Entity Framework 假设 RuleViolation 是一种复杂类型,并试图将其映射到不存在的 RuleViolations 数据库表。使用modelBuilder.Ignore&lt;RuleViolation&gt;() 告诉实体框架忽略这种类型。这样做是完全可以接受的。
    【解决方案2】:

    你也可以使用:

    [NotMapped]
    public IList<RuleViolation> RuleViolations { get; set; }
    

    要使用NotMapped,您必须添加using System.ComponentModel.DataAnnotations;

    编辑:

    现在我看到你想避免映射基类的属性。它不适用于 OnModelCreating - 在 CTP5 中已确认存在错误(稍后我将尝试查找链接)。我不确定它是否适用于 NotMappedAttribute。此属性只是实现相同结果的其他方法。

    【讨论】:

    • 我决定接受 Dave 关于使用 OnModelCreating 的建议。为什么你建议使用 NotMapped 而不是 OnModelCreating?即使我按照 Dave 的建议进行操作,我仍然会收到错误消息。你能看看我更新的帖子吗?
    • 我使用了modelBuilder.Ignore();它可以工作,但就像我说的我觉得使用它不舒服,我更喜欢忽略。我认为现在我将排除从我的 Entity 类派生并将 RuleViolations 属性移动到我的 News 类,然后 Ignore 完美地工作。就目前而言,直到错误修复为止。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-11
    • 1970-01-01
    • 1970-01-01
    • 2012-10-11
    • 1970-01-01
    • 1970-01-01
    • 2012-02-25
    相关资源
    最近更新 更多