use*_*154 3 .net c# entity-framework
我有一个从数据库生成的实体框架模型.其中一个实体是'Session'和'Type'int属性.
自动生成的课程:
public class Session
{
int Type { get; set;}
}
Run Code Online (Sandbox Code Playgroud)
EDMX:
<EntityType Name="Sessions">
<Property Name="Type" Type="int" Nullable="false" />
</EntityType>
Run Code Online (Sandbox Code Playgroud)
有时在加载数据库值时,我得到一个异常,说它不能将'Type'属性(这是int)设置为'string'值:
System.InvalidOperationException:'Session'上的'Type'属性无法设置为'System.String'值.您必须将此属性设置为类型为"System.Int32"的非null值.位于System.Data.Entity.Core.Common.Internal.Materialization.Coordinator的
1.GetValue(DbDataReader reader, Int32 ordinal) at System.Data.Entity.Core.Common.Internal.Materialization.Shaper.GetPropertyValueWithErrorHandling[TProperty](Int32 ordinal, String propertyName, String typeName) at lambda_method(Closure , Shaper ) at System.Data.Entity.Core.Common.Internal.Materialization.Shaper.HandleEntityAppendOnly[TEntity](Funclambda_method(Closure,Shaper)的System.Data.Entity.Core.Common.Internal.Materialization.Shaper.ErrorHandlingValueReader 2 constructEntityDelegate,EntityKey entityKey,EntitySet entitySet)1.ReadNextElement(Shaper shaper) at System.Data.Entity.Core.Common.Internal.Materialization.Shaper1.SimpleEnumerator.MoveNext()在System.Linq.Enumerable.FirstOrDefault [TSource](IEnumerable的1 source) at System.Linq.Queryable.FirstOrDefault[TSource](IQueryable1个源)
这是失败的查询:
var session = db.Sessions.Include("Game.Mod").Where(s => s.UniqueId == message.SessionUid && s.DomainId == this.DomainId && s.Game.UniqueId == message.GameUid).FirstOrDefault();
Run Code Online (Sandbox Code Playgroud)
在本地调试时,一切正常.这是在部署到prod时.
我目前在sql azure上使用EF 6.1,我认为它可能与升级有关,我不认为这发生在之前(使用6.1).但我可能错了.
数据库列也是一个int(100%确认),映射是正确的.
System.InvalidOperationException:'Session'上的'Type'属性无法设置为'System.String'值.
第一句暗示转换试图在从某一点int到string.但是,您的数据库字段和属性ints.我相信你,但EF尝试转换.
尝试将查询保存到变量并FirstOrDefault()在另一行上执行.
var query = db.Sessions.Include("Game.Mod").Where(s => s.UniqueId == message.SessionUid && s.DomainId == this.DomainId && s.Game.UniqueId == message.GameUid);
var session = query.FirstOrDefault();
Run Code Online (Sandbox Code Playgroud)
然后尝试保存在数据库中执行查询之前生成的SQL.这可能会给你一个线索.
您必须将此值设置为'System.Int32'类型的非null值
为了解决您的问题,.Net建议您将变量设置为非空值.这是因为数据库中不允许使用空值...
您是否尝试将该字段设置为nullable?
不要忘记检查您是否在数据库和代码中进行更改.
此外,我很想冒险说你在生产中面临问题而不是在本地,因为你有不同的数据.不要忘记在尝试对数据执行某些操作时会发生运行时异常.
| 归档时间: |
|
| 查看次数: |
5116 次 |
| 最近记录: |