代码第一个实体框架没有保存到数据库

时间:2014-05-28 00:19:24

标签: c# entity-framework

好的,我正在用实体框架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或计算一样。

2 个答案:

答案 0 :(得分:2)

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

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。这应该是:

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

答案 1 :(得分:1)

您无法将匿名类型类或动态添加到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