简体   繁体   English

无法使用实体框架和 ASP.NET MVC 从 Models.Class 转换为 DbContext.Class

[英]Cannot convert from Models.Class to DbContext.Class using Entity Framework and ASP.NET MVC

I'm using a database first approach and I want to insert some data into the database.我正在使用数据库优先方法,我想将一些数据插入数据库。 I am using this code:我正在使用这段代码:

public ActionResult Create(StudentDetails studentDetails)
{
    using (StudentRecordManagementEntities1 obj = new StudentRecordManagementEntities1())
    {
         obj.StudentDetails.Add(studentDetails);  //throws error
    }
}

Model class: Model class:

public class StudentDetails
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }
    public string Name { get; set; }
    public Nullable<long> ContactNumber { get; set; }
    public string Address { get; set; }
    public decimal Fees { get; set; }
    public bool isPaid { get; set; }
}

Error details:错误详情:

Error CS1503 Argument 1: cannot convert from 'StudentRecordManagement.Models.StudentDetails' to 'StudentRecordManagement.Models.StudentDetail' StudentRecordManagement*错误 CS1503 参数 1:无法从“StudentRecordManagement.Models.StudentDetails”转换为“StudentRecordManagement.Models.StudentDetail”StudentRecordManagement*

If you need more details, kindly let me know.如果您需要更多详细信息,请告诉我。

It looks like your model class is StudentRecordManagement.Models.StudentDetail , however you are inserting StudentRecordManagement.Models.StudentDetail s .看起来您的 model class 是StudentRecordManagement.Models.StudentDetail ,但是您正在插入StudentRecordManagement.Models.StudentDetail So what you can do is to give necessary type to your Database model:所以你可以做的是给你的数据库 model 提供必要的类型:

public ActionResult Create(StudentDetails studentDetails)
{
    using (StudentRecordManagementEntities1 obj = new StudentRecordManagementEntities1())
    {
        var studentDetail = new StudentRecordManagement.Models.StudentDetail {
         Name = studentDetails.Name,
         // the other code is omitted for the brevity
     };
     obj.StudentDetails.Add(studentDetail);  //
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 DBContext类ASP.Net MVC - DBContext class ASP.Net MVC 如何使用ASP.NET MVC 3实体框架中的DBContext映射存储过程 - How to map a stored procedure using DBContext in ASP.NET MVC 3 Entity Framework 当我们使用带有Entity Framework的ASP.NET MVC应用程序开发时,应用类图的位置? - Where to apply class diagram when we develop using ASP.NET MVC application with Entity Framework? ASP.NET使用实体框架5.X类 - ASP.NET Using Entity Framework 5.X Class 将 DbContext 解析为派生类的 ASP.NET MVC 问题 - ASP.NET MVC issue resolving DbContext to a Derived class ASP.NET MVC 3-未执行DBContext类 - ASP.NET MVC 3 - DBContext Class Not Being Executed 使用实体框架和标识分离 ASP.NET MVC 中的模型 - Separating Models in ASP.NET MVC with Entity Framework and Identity 实体框架和ASP.NET MVC更复杂的模型 - Entity Framework & ASP.NET MVC more complex models 在不使用实体框架的情况下,仅将来自不同模型的必需字段/属性调用到 asp.net mvc 5 中的视图模型中 - call only required field/attribute from the different models into a view model in asp.net mvc 5 without using entity framework ASP.NET将类转换为&#39;System.Data.Entity.Infrastructure.DbSqlQuery <Project.Models.HomesClass> &#39; - ASP.NET Convert Class to 'System.Data.Entity.Infrastructure.DbSqlQuery<Project.Models.HomesClass>'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM