Insert new record into database using Entity Framework c# entity-framework


I have a table in my database like this:

[id] [uniqueidentifier] NOT NULL,
[user_id] [uniqueidentifier] NOT NULL,
[download_type] [nvarchar](50) NOT NULL,
[download_id] [nvarchar](50) NOT NULL,
[download_date] [datetime] NOT NULL,
[user_ip_address] [nvarchar](20) NOT NULL,

The id is defined to be the primary key.

I want to insert a new record into this table. Here is my code I cannot get to work.

CustomerPortalEntities_Customer_Downloads dbcd = new CustomerPortalEntities_Customer_Downloads();

public ActionResult Download(string id)
    var collection = new FormCollection();
    collection.Add("user_id", Membership.GetUser().ProviderUserKey.ToString());
    collection.Add("download_type", "Car");
    collection.Add("download_id", id);
    collection.Add("download_date", DateTime.Now.ToString());
    collection.Add("user_ip_address", Request.ServerVariables["REMOTE_ADDR"]);            


    return Redirect("../../Content/files/" + id + ".EXE");

The errors I get is on the line dbcd.AddToCustomer_Downloads(collection);

The best overloaded method match for 'CustomerPortalMVC.Models.CustomerPortalEntities_Customer_Downloads.AddToCustomer_Downloads(CustomerPortalMVC.Models.Customer_Downloads)' has some invalid arguments

Argument '1': cannot convert from 'System.Web.Mvc.FormCollection' to 'CustomerPortalMVC.Models.Customer_Downloads'

What do I need to change to make this work?

6/19/2012 9:03:10 PM

Accepted Answer

You need to provide an object of type CustomerPortalMVC.Models.Customer_Downloads to method AddToCustomer_Downloads and then call SaveChanges on your data context like this:

public ActionResult Download(string id) 
    var item = new CustomerPortalMVC.Models.Customer_Downloads(); 
    item.user_id = Membership.GetUser().ProviderUserKey.ToString(); 
    item.download_type = "Car"; 
    item.download_id = id; 
    item.download_date = DateTime.Now.ToString(); 
    item.user_ip_address = Request.ServerVariables["REMOTE_ADDR"];             
    return Redirect("../../Content/files/" + id + ".EXE"); 
6/19/2012 9:03:34 PM

Popular Answer

You need to create an instance of your Customer_Downloads class and pass that to your AddToCustomer_Downloads method. The error message is telling you that the interface for that method expects a Customer_Downloads object but you are passing it a FormCollection object.

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow