Reputation: 4259
public JsonResult CreateNewMember(MembersModel m)
{
try
{
using (var db = new DbEntityDataModel())
{
***var EmpAdd = db.tblMembers.Add(m);***
return Json(new { Result = "Error", Records = EmpAdd });
}
}
catch (Exception EX)
{
return Json(new { Result = "Error", Message = EX });
}
}
I get this error while inserting into db. I have highlighted the part
Cannot convert from 'Mvcapps.Models.MembersModel' to 'Mvcapps.Models.tblMember'
The other day I have written like this but it worked
public JsonResult CreateRecord(Person objperson)
{
try
{
using (var db = new VIVEK_ROOPASOFTEntities1())
{
var addedPerson = db.Persons.Add(objperson);
db.SaveChanges();
return Json(new { Result = "OK", Records = addedPerson }, JsonRequestBehavior.AllowGet);
}
}
public class MembersModel
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Gender { get; set; }
public string Mobile { get; set; }
public string Email { get; set; }
public string Pasword { get; set; }
public string Country { get; set; }
public string City { get; set; }
public string PinCode { get; set; }
}
Here is tblMember
namespace Mvcapps.Models
{
using System;
using System.Collections.Generic;
public partial class tblMember
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Gender { get; set; }
public string Mobile { get; set; }
public string Email { get; set; }
public string Pasword { get; set; }
public string Country { get; set; }
public string City { get; set; }
public string PinCode { get; set; }
}
}
Upvotes: 0
Views: 1508
Reputation: 7824
In response to your comment on the question itself. Entity Framework represents database tables as classes in your project. The error message you're receiving is telling you that EF doesn't know how to turn MembersModel into a tblMember so that it can then save that data to the DB.
As @marc_s says you need to convert from MembersModel to tblMember yourself.
Assuming that you already know that the MembersModel you receive is definitely new and you don't have to check that it exists. I'd do something like
public JsonResult CreateNewMember(MembersModel m)
{
try
{
var newTblMember = new tblMember
{
Id = m.Id,
FirstName = m.FirstName,
LastName = m.LastName,
// and so on for the other properties
};
using (var db = new DbEntityDataModel())
{
var EmpAdd = db.tblMembers.Add(newTblMember);
return Json(new { Result = "Error", Records = EmpAdd });
}
}
catch (Exception EX)
{
return Json(new { Result = "Error", Message = EX });
}
}
Because db.tblMembers is a collection of type tblMember and there is no implicit or explicit conversion between MembersModel and tblMember you have to convert it yourself. You can create conversions yourself if you want to
Upvotes: 1
Reputation: 755023
The db.tblMembers
is a collection of type tblMember
- what you're trying to store into it is of type MembersModel
- that's not the same thing (not the same class).
That's exactly what the error says - it cannot automatically convert something from MembersModel
(your parameter m
) into tblMember
- you'll need to do the conversion yourself!
Upvotes: 1