简体   繁体   English

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

[英]code first Entity Framework not saving to database

Ok, I am doing a project with entity framework 6. I have my class laid out. 好的,我正在做一个带有实体框架6的项目。 When I try to add the information to the database; 当我尝试将信息添加到数据库时; it gives me following errors: 它给了我以下错误:

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'

Here is my controller: 这是我的控制器:

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();
    }    
}

and here is the model: 这是模型:

    [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; }

Also, how do I generate a number automatically for the "Cust_Id" like a database do with the sql code IDENTITY or computed. 另外,如何像SQL数据库IDENTITY或计算的那样为数据库自动为“ Cust_Id”生成一个数字。

You have 2 problems. 您有2个问题。 First, this line is wrong: 首先,这一行是错误的:

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

You are creating an anonymous type instead of a customer object. 您正在创建匿名类型而不是customer对象。 Try this instead: 尝试以下方法:

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

Secondly your context DefaultConnection is wrong and contains this: 其次,您的上下文DefaultConnection错误,并且包含以下内容:

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

You are creating a DbSet of your context class instead of customer s. 您正在创建上下文类的DbSet而不是customer This should be: 应该是:

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

You cannot add Anonymous typed class or Dynamic to a DbSet so you need to create an instance class of customer in order to be added to your DbSet. 您不能将Anonymous类型的类或Dynamic添加到DbSet中,因此您需要创建一个Customer的实例类才能将其添加到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();
    }    
}

Also your DbContext.cs class should have this instead of your code: 同样,您的DbContext.cs类应具有以下内容而不是代码:

public DbSet<Customer> Customers { get; set; }

For the generation of primary key you should use this: 为了生成主键,您应该使用以下命令:

[DatabaseGenerated(DatabaseGeneratedOption.Identity)]

Make sure you try this tutorial first: http://msdn.microsoft.com/en-us/data/jj572366.aspx 确保首先尝试本教程: http : //msdn.microsoft.com/zh-cn/data/jj572366.aspx

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM