Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extend an Entity Framework 6 model

I am trying to extend an EF 6 code first model and I am getting an error. I have searched and can't find much information on the issue. I have created a partial class of the same name as my model in the same namespace. Here is my model and the extension:

Model -

namespace DataAccess.Models
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.DataAnnotations.Schema;
    using System.Data.Entity.Spatial;

    [Table("Vehicle")]
    public partial class Vehicle
    {
        public Vehicle()
        {
            WorkOrders = new HashSet<WorkOrder>();
        }

        public int VehicleID { get; set; }

        public int CustomerID { get; set; }

        [StringLength(17)]
        public string VIN { get; set; }

        public int Mileage { get; set; }

        [StringLength(4)]
        public string Year { get; set; }

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

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

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

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

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

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

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

        public bool IsDeleted { get; set; }

        [StringLength(25)]
        public string License { get; set; }

        public bool? Is4WD { get; set; }

        [StringLength(25)]
        public string Color { get; set; }

        public string Notes { get; set; }

        [StringLength(2)]
        public string LicenseState { get; set; }

        public virtual Customer Customer { get; set; }

        public virtual ICollection<WorkOrder> WorkOrders { get; set; }
    }
}

Extension -

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataAccess.Models
{
    public partial class Vehicle
    {
        public string CustomerName { get; set; }
    }
}

The error I am getting is:

{"The model backing the 'AiContext' context has changed since the database was created. Consider using Code First Migrations to update the database (http://go.microsoft.com/fwlink/?LinkId=238269)."}

I am getting this error when trying to call a stored procedure. More specifically, it is being thrown on this line:

using (ObjectContext objectContext = ((System.Data.Entity.Infrastructure.IObjectContextAdapter)dbContext).ObjectContext)

of this method:

public List<Vehicle> SearchVehicles(string vin,
                                            string license,
                                            string year,
                                            string make,
                                            string model)
        {
            SqlParameter vinParameter;
            if (vin != null)
            {
                vinParameter = new SqlParameter("vin", vin);
            }
            else
            {
                vinParameter = new SqlParameter("vin", "");
            }

            SqlParameter licenseParameter;
            if (license != null)
            {
                licenseParameter = new SqlParameter("license", license);
            }
            else
            {
                licenseParameter = new SqlParameter("license", "");
            }

            SqlParameter yearParameter;
            if (year != null)
            {
                yearParameter = new SqlParameter("year", year);
            }
            else
            {
                yearParameter = new SqlParameter("year", "");
            }

            SqlParameter makeParameter;
            if (make != null)
            {
                makeParameter = new SqlParameter("make", make);
            }
            else
            {
                makeParameter = new SqlParameter("make", "");
            }

            SqlParameter modelParameter;
            if (model != null)
            {
                modelParameter = new SqlParameter("model", model);
            }
            else
            {
                modelParameter = new SqlParameter("model", "");
            }

            using (AiContext dbContext = new AiContext())
            {
                using (ObjectContext objectContext = ((System.Data.Entity.Infrastructure.IObjectContextAdapter)dbContext).ObjectContext)
                {
                    return objectContext.ExecuteStoreQuery<Vehicle>("SearchVehicles  @VIN, " +
                                                                    "@License, @Year, " +
                                                                    "@Make, @Model",
                                                                    vinParameter,
                                                                    licenseParameter,
                                                                    yearParameter,
                                                                    makeParameter,
                                                                    modelParameter).ToList();
                }
            }
        }

I am familiar with the error. EF obviously thinks I've made a change to the model and wants me to update the database. Any ideas on why I am getting this error or another way to extend a model to add properties or business logic? Thanks for the help in advance.

like image 838
flyNflip Avatar asked Feb 12 '23 01:02

flyNflip


1 Answers

If you use it only in the business logic and not intended to map it as a column in the database. You need to add [NotMapped] attribute.

[NotMapped]
public string CustomerName { get; set; }
like image 111
Yuliam Chandra Avatar answered Feb 14 '23 13:02

Yuliam Chandra