EF 6在字符串属性中设置bool

时间:2014-10-09 10:49:37

标签: c# entity-framework asp.net-web-api

我使用EF6与通用UoW和存储库模式。我通过以下方式从数据库中找到记录:

public virtual T Get(Expression<Func<T, bool>> where)
{
   return dbset.Where(where).FirstOrDefault<T>();
}

但是,在一个实体上,我得到以下异常。我已经检查并重新检查了db,model,dto等,属性x绝对是一个字符串。这是生成的模型:

public partial class MyEntity
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Number { get; set; }
    public int ForeignTableId { get; set; }
    public string Comments { get; set; }
    public string x { get; set; } // <-- This is the field causing the problem
    public Nullable<bool> IsDeleted { get; set; }
    public virtual ForeignTable ForeignTable { get; set; }
}

我的控制器操作调用该服务:

MyEntity entity = myEntityService.Get(x => x.Number == param);

服务调用存储库:

public MyEntity Get(Expression<Func<MyEntity , bool>> where)
{
    return myEntityRepository.Get(where);
}

EF搞砸了还是我错过了其他什么?

  

&#34;消息&#34;:&#34;发生了错误。&#34;,&#34; ExceptionMessage&#34;:&#34;&#39; x&#39;物业在&#39; y&#39;无法设置为System.Boolean&#39;值。您必须将此属性设置为类型为&#39; System.String&#39;的非null值。 &#34;&#34; ExceptionType&#34;:&#34; System.InvalidOperationException&#34;&#34;堆栈跟踪&#34;:&#34;在System.Data.Entity.Core.Common.Internal.Materialization.Shaper.ErrorHandlingValueReader 1.GetValue(DbDataReader reader, Int32 ordinal)\r\n at lambda_method(Closure , Shaper )\r\n at System.Data.Entity.Core.Common.Internal.Materialization.Shaper.HandleEntityAppendOnly[TEntity](Func 2 constructEntityDelegate,EntityKey entityKey,EntitySet entitySet)\ r \ n在lambda_method(Closure,Shaper)\ r \ n在System。 Data.Entity.Core.Common.Internal.Materialization.Coordinator 1.ReadNextElement(Shaper shaper)\r\n at System.Data.Entity.Core.Common.Internal.Materialization.Shaper 1.SimpleEnumerator.MoveNext()\ r \ n在System.Data.Entity.Internal.LazyEnumerator 1.MoveNext()\r\n at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable 1 source)\ r \ n在System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.b__1 [TResult](IEnumerable 1 sequence)\r\n at System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.ExecuteSingle[TResult](IEnumerable 1查询,Expression queryRoot)\ r \ n在System.Data.Entity.Core.Objects.ELinq。在System.Linq的System.Data.Entity.Internal.Linq.DbQueryProvider.Execute [TResult](表达式表达式)\ r \ n中的ObjectQueryProvider.System.Linq.IQueryProvider.Execute [TResult](表达式表达式)\ r \ n。 Queryable.FirstOrDefault [TSource](IQueryable`1 source)

0 个答案:

没有答案