实体框架生成无效的列名称

时间:2018-06-18 13:48:45

标签: .net entity-framework-6

我在使用Entity Framework映射某些表时遇到问题。我在现有数据库上使用代码优先方法。

在我的数据模型中,我有

    modelBuilder.Entity<Credential>()
        .HasRequired(e => e.User)
        .WithMany()
        .HasForeignKey(e => e.ID_User).WillCascadeOnDelete(false);

在用户域模型中我有

[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public User()
{
    CommentsInsertUser = new HashSet<Comment>();
    CommentsUpdateUser = new HashSet<Comment>();
    CondosInsertUser = new HashSet<Condo>();
    CondosUpdateUser = new HashSet<Condo>();
    ContentsInsertUser = new HashSet<Content>();
    ContentsUpdateUser = new HashSet<Content>();
    //Credentials = new HashSet<Credential>();
    Flats = new HashSet<Flat>();
    Flats1 = new HashSet<Flat>();
    Flats2 = new HashSet<Flat>();
    Flats3 = new HashSet<Flat>();
    Logs = new HashSet<Log>();
    Menus = new HashSet<Menu>();
    Menus1 = new HashSet<Menu>();
    Stairs = new HashSet<Stair>();
    Stairs1 = new HashSet<Stair>();
    // UserInfos = new HashSet<UserInfo>();
    UserInfos1 = new HashSet<UserInfo>();
    UserInfos2 = new HashSet<UserInfo>();
    Roles = new HashSet<Role>();
}

[Key]
public long ID_Users { get; set; }

[Required]
[StringLength(50)]
public string Account { get; set; }

[Required]
[StringLength(100)]
public string Name { get; set; }

[Required]
[StringLength(150)]
public string LastName { get; set; }

[Required]
[StringLength(200)]
public string Email { get; set; }

[Required]
[StringLength(500)]
public string Andress { get; set; }

[Required]
[StringLength(150)]
public string City { get; set; }

public int ID_Province { get; set; }

public int ID_Region { get; set; }

[Required]
[StringLength(50)]
public string PostalCode { get; set; }

public DateTime? Birthday { get; set; }

public long? ID_User_Insert { get; set; }

[Required]
public DateTime DTInsert { get; set; }

public long? ID_User_Update { get; set; }
public DateTime? DTUpdate { get; set; }

[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Comment> CommentsInsertUser { get; set; }

[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Comment> CommentsUpdateUser { get; set; }

[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Condo> CondosInsertUser { get; set; }

[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Condo> CondosUpdateUser { get; set; }

[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Content> ContentsInsertUser { get; set; }

[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Content> ContentsUpdateUser { get; set; }

public virtual Credential Credential { get; set; }

在Credential域模型中我有

public partial class Credential
{
        [Key]
        public int ID_Credentials { get; set; }

        public long ID_User { get; set; }

        [Required]
        [StringLength(100)]
        public string Password { get; set; }

        public Guid? SessionKey { get; set; }

        public Guid? RecoveryKey { get; set; }

        [StringLength(150)]
        public string SecretQuestion { get; set; }

        [StringLength(150)]
        public string SecretAnswer { get; set; }

        [Required]
        public DateTime DTInsert { get; set; }

        public DateTime? DTUpdate { get; set; }

        public virtual User User { get; set; }
}

当我在User表上执行读取操作时,出现以下错误:

  

System.Data.SqlClient.SqlException(0x80131904):列名称'Credential_ID_Credentials'无效。

     System.Data.SqlClient.SqlCommand中的

。&lt;&gt; c.b__180_0(任务`1结果)

     System.Threading.Tasks.ContinuationResultTaskFromResultTask`2.InnerInvoke()中的

  在System.Threading.Tasks.Task.Execute()

中      

---精致的traccia dello stack da posizione precedentedoveèstatagenerata l'eccezione ---

     System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess中的

(任务任务)
  在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)
  在System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition.d__c.MoveNext()

老实说,我无法理解我在映射中的错误;知识渊博的人可以帮助我吗?

2 个答案:

答案 0 :(得分:0)

我认为你的关系图是错误的,你可以试试下面的代码

modelBuilder.Entity<Credential>()
            .HasRequired<User>(s => s.ID_User)
            .WithMany(g => g.Credentials)
            .HasForeignKey<long>(s => s.ID_User).WillCascadeOnDelete(false);

答案 1 :(得分:0)

我更改凭据对象属性的映射对象用户并为我工作!

[NotMapped]
public virtual Credential Credential { get; set; }