添加附加属性的实体框架4

添加附加属性的实体框架4

本文介绍了添加附加属性的实体框架4 code第一CTP 5实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 ASP.NET MVC 3 实体框架code第一CTP 5 。我在想,如果有可能添加不映射到表列的附加属性?

我haved一个新闻类,它的定义是这样的:

 公共类新闻:实体
{
   公众诠释NewSID的{搞定;组; }
   公共字符串名称{搞定;组; }
   公共字符串车身{搞定;组; }
   公共BOOL活跃{搞定;组; }
}

我的数据库上下文类:

 公共类MyContext:的DbContext
{
   公共DbSet<新闻> Newses {搞定;组; }
}

在实体类我已经定义像一个属性:

 公开的IList< RuleViolation> RuleViolations {搞定;组; }

我没有code这部分,但是我希望当对象被确认全碎了的规则被添加到这个列表。那我得到的错误是:

 模型生成过程中检测到一个或多个验证错误:    System.Data.Edm.EdmEntityType:的EntityType'RuleViolation没有定义键。定义此的EntityType的关键。
    System.Data.Edm.EdmEntitySet:的EntityType:该EntitySet的RuleViolations是基于一个没有定义的键型RuleViolation。

下面是我的reposity code:

 公共新闻FindById(INT NewSID的)
{
   返回context.Database.SqlQuery<新闻>(News_FindById @NewsId
      新的SqlParameter(NewSID的NewSID的))FirstOrDefault()。
}

更新2011-03-02:

下面是我的实体类:

 公共类实体
{
   公众的IList< RuleViolation> RuleViolations {搞定;组; }   公共BOOL验证()
   {
      //仍然需要codeD
      BOOL的isValid = TRUE;      返回的isValid;
   }
}

下面是我的 RuleViolation 类:

 公共类RuleViolation
{
   公共RuleViolation(字符串参数名称,字符串的errorMessage)
   {
      参数名称=参数名称;
      的ErrorMessage =的errorMessage;
   }   公共字符串参数名称{搞定;组; }
   公共字符串的ErrorMessage {搞定;组; }
}

下面是我的上下文类:

 公共类MyContext:的DbContext
{
   公共DbSet<新闻> Newses {搞定;组; }   保护覆盖无效OnModelCreating(模型构建器模型构建器)
   {
      modelBuilder.Entity<新闻>()忽略(N => n.RuleViolations)。
   }
}


解决方案

您可以用流利的API通过添加一个无视规则,你的 OnModelCreating 方法忽略类型的 MyContext

 公共类MyContext:{的DbContext  公共DbSet<新闻> Newses {搞定;组; }  保护覆盖无效OnModelCreating(模型构建器制造商){    builder.Ignore< RuleViolation>()  }}

或者,您可以使用 NotMapped 属性忽略的属性。

 公共类Enitity {  [NotMapped]
  公众的IList< RuleViolation> RuleViolations {搞定;组; }  这里//其他属性}

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

I am using ASP.NET MVC 3 and Entity Framework code first CTP 5. I was wondering if it is possible to add additional properties that is not mapped to a table column?

I haved a News class and it is defined as such:

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

My database context class:

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

In the entity class I have a property defined like:

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

I have not code this part yet, but I want all broken rules to be added to this list when the object is validated. The error that I am getting is:

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.

Here is my reposity code:

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

UPDATE 2011-03-02:

Here is my Entity class:

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

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

      return isValid;
   }
}

Here is my RuleViolation class:

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

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

Here is my context class:

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

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

You can ignore the type using Fluent API by adding an ignore rule to your OnModelCreating method of your MyContext class

public class MyContext : DbContext {

  public DbSet<News> Newses { get; set; }

  protected override void OnModelCreating(ModelBuilder builder) {

    builder.Ignore<RuleViolation>()

  }

}

Or you can ignore the property by using the NotMapped attribute

public class Enitity {

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

  //other properties here

}

and then Entity Framework will ignore the property.

这篇关于添加附加属性的实体框架4 code第一CTP 5实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 15:09