[英]Calling stored procedure with “FromSqlRaw” command returning an error
我在類似的問題中看到了這個錯誤,但這是我第一次嘗試.Net Core,我想確保我的 c# 代碼不是罪魁禍首。 通過FromSqlRaw
命令調用存儲過程時出現錯誤。 我確認我正在向FromSqlRaw
命令發送正確的值,但錯誤是在抱怨一個鍵?
請看下面的代碼:
Controller
public class HomeController : Controller
{
private IStudent _info;
private readonly StudentInformation _studentInformation;
public HomeController(IStudent info)
{
_info = info;
}
//public async Task<IActionResult> Index([FromQuery] Student student)
public IActionResult Index([FromQuery] Student student)
{
int id = student.Id;
if (id == 0)
{
return NotFound();
}
var model = _info.GetById(id);
return View(model);
}
}
學生 Class
public class Student
{
public int Id_Num { get; set; }
public string StudentName { get; set; }
public string PreferredName { get; set; }
public string Gender { get; set; }
public string DOB { get; set; }
public string Resident { get; set; }
public string F1Visa { get; set; }
public string Division { get; set; }
public string Tuition { get; set; }
public string cohort { get; set; }
public string Ferpa { get; set; }
}
public class StudentInformationService : IStudent
{
private StudentInfo _info;
public StudentInformationService(StudentInfo info)
{
_info = info;
}
public IEnumerable<Student> GetAll()
{
throw new NotImplementedException();
}
public Student GetById(int id)
{
return _info.StudentRow
.FromSqlRaw("StudentRegistrationInfo @IdNum, @OldIdNum",
new SqlParameter("IdNum", id),
new SqlParameter("OldIdNum", id))
.AsEnumerable().FirstOrDefault();
}
}
這是錯誤:
InvalidOperationException:實體類型“學生”需要定義主鍵。 如果您打算使用無密鑰實體類型,請調用“HasNoKey()”。
Microsoft.EntityFrameworkCore.Infrastructure.ModelValidator.ValidateNonNullPrimaryKeys(IModel model,IDiagnosticsLogger 記錄器)
Microsoft.EntityFrameworkCore.Infrastructure.ModelValidator.Validate(IModel model,IDiagnosticsLogger 記錄器)
Microsoft.EntityFrameworkCore.Infrastructure.RelationalModelValidator.Validate(IModel model,IDiagnosticsLogger 記錄器)
Microsoft.EntityFrameworkCore.SqlServer.Internal.SqlServerModelValidator.Validate(IModel model,IDiagnosticsLogger 記錄器)
Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.ConventionDispatcher+ImmediateConventionScope.OnModelFinalized(IConventionModelBuilder modelBuilder)
Microsoft.EntityFrameworkCore.Metadata.Internal.Model.FinalizeModel()
Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.GetModel(DbContext 上下文,IConventionSetBuilder 約定SetBuilder)
Microsoft.EntityFrameworkCore.Internal.DbContextServices.CreateModel()
Microsoft.EntityFrameworkCore.Internal.DbContextServices.get_Model()
Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitFactory(FactoryCallSite factoryCallSite,RuntimeResolverContext 上下文)
Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor.VisitCallSiteMain(ServiceCallSite callSite,TArgument 參數)
Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite callSite,RuntimeResolverContext 上下文,ServiceProviderEngineScope serviceProviderEngine,RuntimeResolverLock lockType)
Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScopeCache(ServiceCallSite singletonCallSite,RuntimeResolverContext 上下文)
Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor.VisitCallSite(ServiceCallSite callSite,TArgument 參數)
Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite,RuntimeResolverContext 上下文)
Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor.VisitCallSiteMain(ServiceCallSite callSite,TArgument 參數)
Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite callSite,RuntimeResolverContext 上下文,ServiceProviderEngineScope serviceProviderEngine,RuntimeResolverLock lockType)
Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScopeCache(ServiceCallSite singletonCallSite,RuntimeResolverContext 上下文)
Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor.VisitCallSite(ServiceCallSite callSite,TArgument 參數)
Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.Resolve(ServiceCallSite callSite,ServiceProviderEngineScope 范圍)
Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider 提供程序,類型 serviceType)
Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider 提供程序)
Microsoft.EntityFrameworkCore.DbContext.get_DbContextDependencies()
Microsoft.EntityFrameworkCore.DbContext.get_InternalServiceProvider()
Microsoft.EntityFrameworkCore.DbContext.get_DbContextDependencies()
Microsoft.EntityFrameworkCore.DbContext.get_Model()
Microsoft.EntityFrameworkCore.Internal.InternalDbSet.get_EntityType()
Microsoft.EntityFrameworkCore.Internal.InternalDbSet.get_EntityQueryable()
Microsoft.EntityFrameworkCore.Internal.InternalDbSet.System.Linq.IQueryable.get_Provider()
Microsoft.EntityFrameworkCore.RelationalQueryableExtensions.FromSqlRaw(DbSet 源,字符串 sql,Object[] 參數)
StudentServices.StudentInformationService.GetById(int id) 在 StudentInformationService.cs +
HomeController.cs 中的 StudentChecklist.Controllers.HomeController.Index(Student student) +
lambda_method(閉包,object,對象[])
Microsoft.Extensions.Internal.ObjectMethodExecutor.Execute(對象目標,Object[] 參數)
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker+d__12.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任務任務)
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker+d__10.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ActionExecutedContext 上下文)
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker+d__14.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任務任務)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker+d__22.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext 上下文)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker+d__17.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任務任務)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker+d__15.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任務任務)
Microsoft.AspNetCore.Builder.RouterMiddleware+d__4.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任務任務)
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware+d__7.MoveNext()
錯誤非常清楚 - 您的Student
model class沒有定義實體的主鍵(它應該 - 總是)。
按照慣例, Id
或(classname)Id
(這里: StudentId
)將自動被視為主鍵 - 因為您沒有這些,您需要顯式注釋要用作主鍵的列 - 很可能是Id_Num
列 - 與一個[Key]
注釋 - 像這樣:
public class Student
{
[Key]
public int Id_Num { get; set; }
// remainder of your properties
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.