Wednesday 28 December 2016

Getting SqlException "Invalid column name 'User_Id' from EF4 code-only

case 1- you need to check your current domain model and mapping.
   (i) May be you added two times same property/ column name,
   (ii) you may add any FK which name is changed in your child  table, then you need to add fluent API

  example :-

 suppose your domain class

Parent class

public class User : Entity<int>, IAggregateRoot
    {
        public User()
        {
            Events = new Collection<IDomainEvent>();
        }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public virtual ICollection<ErrorLog> ErrorLogs { get; set; }     
    }

Child class

public class ErrorLog : Entity<int>, IAggregateRoot
    {
        public DateTime ErrorDate { get; set; }
    
        public string ErrorDescription { get; set; }     
        public int? ByUserId{ get; set; }   
        public virtual User User { get; set; }
    }

       Property(t => t.UserId).HasColumnName("UserId");

  HasOptional(t => t.User)
                .WithMany(t => t.ErrorLogs)
                .HasForeignKey(d => d.ByUserId);
----------------------------------------------------------------------------------

case 2 :- You may not have any FK relation with your current domain model, but you mention the collection object in your parent  class, then you need to remove that.






 

No comments:

Post a Comment