代码优先实体框架不保存到数据库

本文关键字:保存 数据库 框架 实体 代码 | 更新日期: 2023-09-27 18:05:32

好的,我正在做一个项目与实体框架6。我把我的班级布置好了。当我试图将信息添加到数据库时;它给了我以下错误:

The best overloaded method match for 'System.Data.Entity.DbSet<img_site_codefi.DAL.DefaultConnection>.Add(img_site_codefi.DAL.DefaultConnection)' has some invalid arguments
Argument 1: cannot convert from 'AnonymousType#1' to 'img_site_codefi.DAL.DefaultConnection'

这是我的控制器:

public ActionResult Contact(customer cust)
{
    try
    {
        if (ModelState.IsValid)
        {
            cust.Tele_comp();
            saveIntoDb(cust); // database
            SendMail(cust); // mail sender
            return RedirectToAction("Submited", "Home");
        }
        return null;
    }
    catch (DataException)
    {
        return View(cust);
    }
}
private void saveIntoDb(customer cust)
{
    using (var cust_In = new DefaultConnection())
    { 
        var customer = new {fname = cust.fname,lname = cust.lname, tele = cust.tele, email = cust.email, reasn = cust.reasn };
        //cust_In.customers.Add(customer); //HERE IS THE ERROR!!!
        cust_In.SaveChanges();
    }    
}

模型如下:

    [Key]
    [] // how to assign a number automatically 
    public int Cust_Id { get; set; } 
    [Required(ErrorMessage = "first name is required!")]
    [Display(Name = "First name")]
    public string fname { get; set; }
    [Display(Name = "Last name")]
    public string lname { get; set; }
    [Required(ErrorMessage = "area code is required!")]
    [StringLength(3)]
    [RegularExpression(@"^[0-9]{3,}$", ErrorMessage = "Minimum 3 numbers required & contain only numbers")]
    [Display(Name = "Telephone")]
    public string tel_area { get; set; }
    [Required(ErrorMessage = "first three numbers are required!")]
    [StringLength(3)]
    [RegularExpression(@"^[0-9]{3,}$", ErrorMessage = "Minimum 3 numbers required & contain only numbers")]
    public string fir_thr_tel { get; set; }
    [Required(ErrorMessage = "last four numbers are required!")]
    [StringLength(4)]
    [RegularExpression(@"^[0-9]{4,}$", ErrorMessage = "Minimum 4 numbers required & contain only numbers")]
    public string lst_fur_tel { get; set; }
    [Required(ErrorMessage = "E-mail is required!")]
    [RegularExpression("^[a-zA-Z0-9_''.-]+@([a-zA-Z0-9-]+''.)+[a-zA-Z]{2,6}$", ErrorMessage = "E-mail is not valid")]
    [Display(Name = "Email")]
    public string email { get; set; }
    [Required(ErrorMessage = "A reason is required!")]
    [Display(Name = "Reason")]
    public string reasn { get; set; }
    public string tele { get; set; }

另外,我如何为"Cust_Id"自动生成一个数字,就像数据库使用sql代码IDENTITY或computed.

代码优先实体框架不保存到数据库

你有两个问题。首先,这一行是错误的:

var customer = new {fname = cust.fname,lname = cust.lname, tele = cust.tele, email = cust.email, reasn = cust.reasn };

您正在创建一个匿名类型而不是customer对象。试试这个:

var customer = new customer 
{
    fname = cust.fname,
    lname = cust.lname, 
    tele = cust.tele, 
    email = cust.email, 
    reasn = cust.reasn 
};

其次,你的上下文DefaultConnection是错误的,包含了这个:

public DbSet<DefaultConnection> customers { get; set; }

您正在创建上下文类的DbSet而不是customer s。这应该是:

public DbSet<customer> customers { get; set; }

您不能添加匿名类型类或动态到DbSet,因此您需要创建客户的实例类才能添加到DbSet。

public ActionResult Contact(Customer cust)
{
    try
    {
        if (ModelState.IsValid)
        {
            cust.Tele_comp();
            saveIntoDb(cust); // database
            SendMail(cust); // mail sender
            return RedirectToAction("Submited", "Home");
        }
        return null;
    }
    catch (DataException)
    {
        return View(cust);
    }
}
private void saveIntoDb(Customer cust)
{
    using (var cust_In = new DbContext())
    { 
        var customer = new Customer {fname = cust.fname,lname = cust.lname, tele = cust.tele, email = cust.email, reasn = cust.reasn };
        cust_In.Customers.Add(customer); //HERE IS THE ERROR!!!
        cust_In.SaveChanges();
    }    
}

你的DbContext.cs类也应该有这个而不是你的代码:

public DbSet<Customer> Customers { get; set; }
对于生成主键,您应该使用以下命令:
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]

确保你先尝试这个教程:http://msdn.microsoft.com/en-us/data/jj572366.aspx