繁体   English   中英

访问父C#实体框架的子表时,列名无效

[英]Invalid Column Name when accessing child table of parent c# entity framework

我目前正在开发一个Web应用程序,其中包含有关用户的信息和与信用有关的信息。 我正在使用Asp.Net Identity来管理和处理Web应用程序中用户帐户的创建,并具有其他几个表,这些表包含与用户直接相关的信息。

我已经建立了一个表(IdentityProfile),该表链接到Identity框架中的AspNetUsers表。 然后,每个表都连接到该表,作为到应用程序中用户帐户功能的链接。 请参阅我的ERD:

实体关系图链接

IdentityProfile和UserProfile表具有以下引用约束:UserProfile-> IdentityProfile,基于每个表中的ProfileId主键字段。

我能够使用相应的UserProfile记录成功创建一个Identity Profile记录:

public void CreateUserProfile(string title, string firstname, string lastname, DateTime dob, IdentityUser user)
    {         

        if(user != null) //Given the successfuly creation of a user account.
        {
            Guid profileId = user.Email.ConvertToMD5GUID();


            IdentityProfile identityProfile = new IdentityProfile //Manual Table Association Link (i.e. Between AspNetUsers Table and UserProfile Table).
            {
                Id = user.Id,
                ProfileId = profileId,
            };             

            identityProfile.UserProfile = new UserProfile
            {
                ProfileId = profileId,
                Title = title,
                FirstName = firstname,
                LastName = lastname,
                DOB = dob,
                DateRegistered = DateTime.Now,
                KBAFailed = false,
                CreditTrolleyRatingsRegistrationStatus = false,
                CreditActivityNotificationStatus = true,
                ThirdPartyPartnerNotificationStatus = true,
                CreditOffersNotificationStatus = true
            };

            ConsumerData.IdentityProfiles.Add(identityProfile);

            ConsumerData.SaveChanges();

        }

    }

但是,在访问创建的UserProfile记录时,Identity Profile返回null和SqlException:无效的列名'UserProfile_ProfileId'。 无效的列名“ UserProfile_ProfileId”。

public void CreateCreditFootprint()
    {

        IdentityProfile identityProfile = ConsumerData
           .IdentityProfiles
           .Single(profile
           => profile.Id == CurrentUserId);

        //An issue with the way in which the user profile is instantiated. 

        identityProfile.UserProfile.CreditFootprint = new CreditFootprint  //ERROR OCCURS ON THIS LINE - SPECICIALLY on identityProfile.UserProfile returing null.

        {

            CreditReportId = identityProfile.UserProfile.LastName.ToString().ConvertToMD5GUID(),
            CreditScore = short.Parse(new Random().Next(500, 710).ToString()), //Example score.
            CreditScoreStatus = "Good", //Example status.
            CumulativeDebt = new Random().Next(1000, 10000), //Example debt.
            LastRetrieved = DateTime.Now,

        };

        ConsumerData.SaveChanges();

    }

    //Migrate method over to UserAccountLink class.
    public void CreateCreditApplicationProfile()
    {
        IdentityProfile identityProfile = ConsumerData
           .IdentityProfiles
           .SingleOrDefault(profile
           => profile.Id == CurrentUserId);

        identityProfile.UserProfile.CreditApplicationProfile = new CreditApplicationProfile {

            ApplicationProfileId = identityProfile.UserProfile.FirstName.ToString().ConvertToMD5GUID(),
            ApplicationCount = 0,
            ApplicationsAcceptedCount = 0,
            ApplicationsDeclinedCount = 0,
            PendingApplicationsCount = 0
        };

        ConsumerData.SaveChanges();
    }

所有记录均已在数据库表中成功创建(分别为AspNetUsers,IdentityProfile(LINK)和UserProfile)。 当我找到现有的IdentityProfile记录并尝试访问其UserProfile链接时,将引发该异常-请参阅identityProfile.UserProfile.CreditApplicationProfile行。

任何帮助将不胜感激这一点。

非常感谢,

这是我修改的代码:

public partial class IdentityProfile
{
    public string Id { get; set; }

    [Required]
    [ForeignKey("UserProfile")]
    public System.Guid ProfileId { get; set; }


    public virtual UserProfile UserProfile { get; set; }
}

public partial class UserProfile
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public UserProfile()
    {
        this.IdentityProfiles = new HashSet<IdentityProfile>();
    }

    public System.Guid ProfileId { get; set; }
    public string Title { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public System.DateTime DOB { get; set; }
    public System.DateTime DateRegistered { get; set; }
    public bool RegistrationStatus { get; set; }
    public bool KBAFailed { get; set; }
    public bool CreditActivityNotificationStatus { get; set; }
    public bool ThirdPartyPartnerNotificationStatus { get; set; }
    public bool CreditOffersNotificationStatus { get; set; }

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]

    [InverseProperty("UserProfile")]
    public virtual ICollection<IdentityProfile> IdentityProfiles { get; set; }


    public virtual CreditApplicationProfile CreditApplicationProfile { get; set; }
    public virtual CreditFootprint CreditFootprint { get; set; }
}

更新后错误

无效的列名错误示例

首先,为了加载导航属性,您必须使用.Include() ,除非导航属性未包含在查询中,否则您将获得NULL值。

其次, Invalid column name 'UserProfile_ProfileId'的错误肯定与您的模型和属性映射有关。 您没有提到数据模型,但是任何类型的包含“ Invalid column name”消息的错误都与您的映射有关(外键主键)。

如果是Entity Framework Code First,则可以根据您的模型使用代码打击

public class IdentityProfile 
{
   [Required]
   [ForeignKey("Profile")]
   public System.Guid ProfileId { get; set; }

   public virtual UserProfile Profile { get; set; }
}

public class UserProfile 
{
    [InverseProperty("Profile")]
    public virtual ICollection<IdentityProfile> IdentityProfiles { get; set; }
}

暂无
暂无

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

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