How to extend Entity Framework 6 model

I am trying to extend the first EF 6 code model and I am getting an error. I have searched and cannot find much information on this. I created an incomplete class with the same name as my model in the same namespace. Here is my model and 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:

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

I am getting this error when I try to call a stored procedure. More specifically, it is thrown into 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 clearly thinks that I have made changes to the model and wants me to update the database. Any ideas on why I am getting this error or another way to extend the model to add properties or business logic? Thanks for your help in advance.

+3


source to share


1 answer


If you are only using it in business logic and are not going to display it as a column in the database. You need to add an attribute [NotMapped]

.



[NotMapped]
public string CustomerName { get; set; }

      

+4


source







All Articles