如何通过代码第一种方式在Entity Framework 6中映射和使用存储过程?

时间:2013-07-14 06:35:08

标签: entity-framework stored-procedures entity-framework-6

我有一个ASP.NET MVC项目,我想先使用Entity Framework 6代码。在Entity Framework 6中,我们可以使用存储过程并映射到它们。我使用此代码进行映射,但是当我想创建我的数据库时,我收到此错误:

  

一个或多个实体的验证失败。有关详细信息,请参阅“EntityValidationErrors”属性。

但是当我清理所有存储过程映射时,它会创建我的数据库。

我必须在我的程序中使用存储过程,因此我必须知道如何通过EF 6代码优先映射和使用存储过程?我的代码有什么问题,我在映射时如何调用存储过程?

using System.Data.Entity;

namespace Raja.Models.Context
{
    public class RajaContext : DbContext
    {
        public RajaContext() : base("name=RajaContext")
        {
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            #region SpareParts
            modelBuilder.Entity<Spareparts>()
                        .MapToStoredProcedures(s => s.Update(u => u.HasName("Spareparts_Update").Parameter(P => P.Code, "Code")
               .Parameter(P => P.SparepartName, "SparepartName")
               .Parameter(P => P.Serial, "Serial")
               .Parameter(P => P.ProjectNumber, "ProjectNumber")
               .Parameter(P => P.SparePartType, "SparePartType")
               .Parameter(P => P.Emplacement, "Emplacement")
               //.Parameter(P => P.TakingPlace.TrainID, "TakingPlace")
               .Parameter(P => P.AssemblyTime, "AssemblyTime")
               .Parameter(P => P.ConsumptionFactor, "ConsumptionFactor")
               .Parameter(P => P.Warranty, "Warranty")
               .Parameter(P => P.Qty, "Qty")
               .Parameter(P => P.Cost, "Cost")
               .Parameter(P => P.HasRepair, "HasRepair")
               .Parameter(P => P.Enable, "Enable")
               .Parameter(P => P.Picture, "Picture")
               .Parameter(P => P.Description, "Description")
               .Parameter(P => P.CreatedDate, "CreatedDate")
               .Parameter(P => P.CreateByUser, "CreateByUser")
               .Parameter(P => P.LastModificationDate, "LastModificationDate")
               .Parameter(P => P.LastModifyByUser, "LastModifyByUser")
               .Parameter(P => P.PlaceOfSupply, "PlaceOfSupply"))
                                                     .Delete(d => d.HasName("Spareparts_Delete").Parameter(p => p.SparepartId, "SparepartId"))
                                                     .Insert(i => i.HasName("Spareparts_Insert").Parameter(P => P.SparepartName, "SparepartName")
               .Parameter(P => P.Code, "Code")
               .Parameter(P => P.Serial, "Serial")
               .Parameter(P => P.ProjectNumber, "ProjectNumber")
               .Parameter(P => P.SparePartType, "SparePartType")
               .Parameter(P => P.Emplacement, "Emplacement")
               //.Parameter(P => P.TakingPlace.TrainID, "TakingPlace")
               .Parameter(P => P.AssemblyTime, "AssemblyTime")
               .Parameter(P => P.ConsumptionFactor, "ConsumptionFactor")
               .Parameter(P => P.Warranty, "Warranty")
               .Parameter(P => P.Qty, "Qty")
               .Parameter(P => P.Cost, "Cost")
               .Parameter(P => P.HasRepair, "HasRepair")
               .Parameter(P => P.Enable, "Enable")
               .Parameter(P => P.Picture, "Picture")
               .Parameter(P => P.Description, "Description")
               .Parameter(P => P.CreatedDate, "CreatedDate")
               .Parameter(P => P.CreateByUser, "CreateByUser")
               .Parameter(P => P.LastModificationDate, "LastModificationDate")
               .Parameter(P => P.LastModifyByUser, "LastModifyByUser")
               .Parameter(P => P.PlaceOfSupply, "PlaceOfSupply")));
            #endregion
        }

        public DbSet<Spareparts> Spareparts { get; set; }
    }
}

0 个答案:

没有答案