簡體   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