繁体   English   中英

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

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

dataContext.Entry(this).Property(property).IsModified = true; 导致以下错误。 非外键属性不会发生这种情况。 任何线索为什么?

实体类型“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;
            };
        }
    }

这与可空问题无关,而是与不同类型有关。 发送int而不是double导致此错误。 所以我们把它变成通用的,一切都很好。

导致代码错误

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

通用的

  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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM