Custom properties in partial class (Invalid column name)

asp.net-core c# entity-framework-core visual-studio

Question

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

1
1
6/18/2018 6:46:19 PM

Accepted Answer

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 { } }
2
6/18/2018 6:55:00 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow