5

I created my database by script, then used the scaffold command to create my classes in visual studio, I added a new partial class for an entity (User) in which I added some custom properties (this wont be on the database) whenever I run a Get to this entity, it throws the error Invalid column name for every custom property I set in my partial class.

Exception message: SqlException: Invalid column name 'ResidentialName'. Invalid column name 'UserDocumentCount'. Invalid column name 'UserPaymentCount'.

The partial class created from the database.

public partial class User
    {
        public User()
        {
            UserDocument = new HashSet<UserDocument>();
            UserPayment = new HashSet<UserPayment>();
        }

        public int UserId { get; set; }
        public string UserIdentityId { get; set; }
        public int? ResidentialId { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Email { get; set; }
        public string Telephone { get; set; }
        public string Street { get; set; }
        public string Number { get; set; }
        public int Block { get; set; }
        public int Lot { get; set; }
        public bool Status { get; set; }
        public DateTime RegistrationDate { get; set; }

        public Residential Residential { get; set; }
        public AspNetUsers UserIdentity { get; set; }
        public ICollection<UserDocument> UserDocument { get; set; }
        public ICollection<UserPayment> UserPayment { get; set; }
    }

The partial class I added.

public partial class User
    {
        public string ResidentialName { get { return this.ResidentialId > 0 && this.Residential != null ? this.Residential.Name : String.Empty; } set { } }
        public int UserDocumentCount { get { return this.UserDocument != null ? this.UserDocument.Count : 0; } set { } }
        public int UserPaymentCount { get { return this.UserPayment != null ? this.UserPayment.Count : 0; } set { } }

        public static async Task<List<User>> GetList(KaiozamaDevContext ctx)
        {
            return await ctx.User.ToListAsync();
        }
        public static async Task<User> GetItem(KaiozamaDevContext ctx, int Id)
        {
            return await ctx.User.SingleOrDefaultAsync(m => m.UserId == Id);
        }
        public static async Task<User> GetItem(KaiozamaDevContext ctx, string Id)
        {
            return await ctx.User.SingleOrDefaultAsync(m => m.UserIdentityId == Id);
        }
    }

The error is thrown on any of the Get methods from above.

Greetings!

Further technical details

EF Core version: 2.1.0

Database Provider: Microsoft.EntityFrameworkCore.SqlServer

Operating system: Windows 10

IDE: Visual Studio Community 2017 15.4.1

Jesus Rios
  • 53
  • 3
  • why not show the actual stack trace and error? – T McKeown Jun 18 '18 at 18:50
  • Are you using code first or database first approach? – Ryan Wilson Jun 18 '18 at 18:50
  • ResidentialId is nullable, shouldn't you be using ResidentialId.Value instead? – T McKeown Jun 18 '18 at 18:51
  • 2
    You should be able to decorate your new properties with the NotMapped annotation, please see this related SO article (https://stackoverflow.com/questions/10385248/ignoring-a-class-property-in-entity-framework-4-1-code-first) – Ryan Wilson Jun 18 '18 at 18:52
  • Remove property setters from these properties, or decorate them with `NotMapped` attribute, or use `Ignore` fluent API, or better off, remove these properties at all - they are misleading and will cause inexpected behavior if used in LINQ to Entities query. – Ivan Stoev Jun 18 '18 at 18:57
  • the NotMapped annotation worked in this case, thank you all. – Jesus Rios Jun 18 '18 at 19:02

1 Answers1

11

Add the NotMapped attribute to your properties that are not in the database.

[NotMapped]
public string ResidentialName { get { return this.ResidentialId > 0 && this.Residential != null ? this.Residential.Name : String.Empty; } set { } }
[NotMapped]
public int UserDocumentCount { get { return this.UserDocument != null ? this.UserDocument.Count : 0; } set { } }
[NotMapped]
public int UserPaymentCount { get { return this.UserPayment != null ? this.UserPayment.Count : 0; } set { } }