0

Ok, I am doing a project with entity framework 6. I have my class laid out. 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.

5
  • This is a C# issue, nothing to do with EF. Commented May 28, 2014 at 0:33
  • Where is the problem? Commented May 28, 2014 at 1:37
  • It looks like you lack basic understanding of how to troubleshoot a simple issue. Note that contrary to what you are stating, you get the error not when you "try to add the information to the database", but when you compile your code - this is a compilation error. This ericlippert.com/2014/03/05/how-to-debug-small-programs does not directly apply to your problem but I think it will be very valuable for you level of knowledge. As for you particular compiler error, here is the explanation: msdn.microsoft.com/en-us/library/b66k5sz9%28v=vs.90%29.aspx Commented May 29, 2014 at 7:10
  • Also look at chapter 7.5.3 Overload resolution in c# spec, you can download it here microsoft.com/en-us/download/confirmation.aspx?id=7029 Commented May 29, 2014 at 7:13
  • For your information, I do not know C# as you do. And yes, I'm starting; but, in school they don't teach hands on, only concepts and ... Commented May 30, 2014 at 5:56

2 Answers 2

2

You have 2 problems. 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. 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:

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

You are creating a DbSet of your context class instead of customers. This should be:

public DbSet<customer> customers { get; set; }
Sign up to request clarification or add additional context in comments.

10 Comments

Same one. I added the () to customer to see if it help but no. Is it because I already have a table with the same name?
Then you didn't copy my code as you still have an anonymous type.
Copy the line of code I wrote above and replace the one in your code. If it doesn't work, give us the exact error message.
Error 1 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 C:\Users\Owner\Documents\Visual Studio 2012\Projects\img-site_codefi\img-site_codefi\Controllers\HomeController.cs 72 17 img-site_codefi and Error 2 Argument 1: cannot convert from 'img_site_codefi.Models.customer' to 'img_site_codefi.DAL.DefaultConnection' C:\Users\Owner\Documents\Visual Studio 2012\Projects\img-site_codefi\img-site_codefi\Controllers\HomeController.cs 72 39 img-site_codefi
@OscarA.Peña you are not making it easy to help you. "it giving errors" does not say much. You need to give exact error messages and specify on which line they were generated. Without this, it's very difficult for others to guess what your problem is.
|
1

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.

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:

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

1 Comment

This is how you should write your code. You need to add an object which your dbset knows about. Please try the code. Have you tried to do any of the ef5/6 before starting this implementation?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.