简体   繁体   English

实体类型的属性不能用于具有实体框架的 Nullable1 类型的对象

[英]The property on entity type cannot be used for objects of type Nullable1 with Entity Framework

The line dataContext.Entry(this).Property(property).IsModified = true;dataContext.Entry(this).Property(property).IsModified = true; is causing this error below.导致以下错误。 This doesn't happen with non foreign key properties.非外键属性不会发生这种情况。 Any clue why?任何线索为什么?

The property 'RankingId' on entity type 'TeamOrganizationSeason' cannot be used for objects of type Nullable1 because it is a property for objects of type Nullable1.实体类型“TeamOrganizationSeason”上的属性“RankingId”不能用于 Nullable1 类型的对象,因为它是 Nullable1 类型对象的属性。

    public class TeamOrganizationSeason 
    {
        public int? RankingId { get; set; }

        [ForeignKey("RankingId")]
        public Ranking Ranking { get; set; }

        public void IsUpdated(TeamOrganizationSeason teamOrganizationSeason, DataContext dataContext)
        {
            Update(RankingId, teamOrganizationSeason.RankingId, dataContext, t => t.RankingId);
        }

        private void Update(double? current, double? original, DataContext dataContext, Expression<Func<TeamOrganizationSeason, double?>> property)
        {
            if (current != original)
            {
                var state = dataContext.Entry(this).State;
                dataContext.TeamOrganizationSeasons.Attach(this);
                dataContext.Entry(this).Property(property).IsModified = true;
            };
        }
    }

This had to do not with nullable issues but with different types.这与可空问题无关,而是与不同类型有关。 An int being sent instead of a double was causing this error.发送int而不是double导致此错误。 So we made it generic and all is well.所以我们把它变成通用的,一切都很好。

Causing Code Error导致代码错误

if (!requestedType.IsAssignableFrom(propertyType))
            {
                throw Error.DbEntityEntry_WrongGenericForProp(
                    propertyName, declaringType.Name, requestedType.Name, propertyType.Name);
            }

Generic通用的

  private void Update<T>(double? current, double? original, DataContext dataContext, Expression<Func<TeamOrganizationSeason, T>> property)
            {
                if (current != original)
                {
                    if (dataContext.Entry(this).State == EntityState.Detached)
                    {
                        dataContext.TeamOrganizationSeasons.Attach(this);
                    }
                    dataContext.Entry(this).Property(property).IsModified = true;
                };
            }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检查实体框架类型的属性是否可为空 - How to check if a property of an Entity Framework type is Nullable 实体类型 &#39;xxx&#39; 上的属性 &#39;xxx&#39; 不能标记为可空/可选,因为该属性的类型是 &#39;short&#39;,它不是可空类型 - The property 'xxx' on entity type 'xxx' cannot be marked as nullable/optional because the type of the property is 'short' which is not a nullable type 组合键实体框架7“属性无法添加到实体类型 - Composite key Entity Framework 7 "The Property Cannot Be added to Entity Type 实体框架自定义类型作为属性 - Entity Framework custom type as property 实体类型的实体框架实例无法跟踪 - Entity Framework instance of entity type cannot be tracked 实体框架:将nullable设置为false,但是type仍然可以为null - Entity Framework: Set nullable to false, but type is still nullable “NameOfProperty”不能用作实体类型“NameOfType”的属性,因为它被配置为导航 - "NameOfProperty" cannot be used as a property on entity type 'NameOfType' because it is configured as a navigation 错误:IAsyncEnumerable 不能用于实体框架上 IEnumerable 类型的参数 - Error: IAsyncEnumerable cannot be used for parameter of type IEnumerable on Entity framework “propertyName”不能用作实体类型“typeName”的属性,因为它被配置为导航 - 'propertyName' cannot be used as a property on entity type 'typeName' because it is configured as a navigation 实体框架不可为空的列映射到可为空的实体属性 - Entity Framework Non-nullable column is mapped to a nullable entity property
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM