Insert new record into database using Entity Framework

asp.net-mvc-2 c# entity-framework

Question

I have a table like this in my database:

[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 described as the main key.

This table needs to have a new record added to it. Here is the code I'm trying to run but can't.

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"]);            

    dbcd.AddToCustomer_Downloads(collection);

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

The line dbcd is where I encounter the problems. 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 must I alter to make this work?

1
1
6/19/2012 9:03:10 PM

Accepted Answer

You must provide an object of typeCustomerPortalMVC.Models.Customer_Downloads to approachAddToCustomer_Downloads then make a callSaveChanges using the data context in this manner:

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"];             
    dbcd.AddToCustomer_Downloads(item); 
    dbcd.SaveChanges(); 
    return Redirect("../../Content/files/" + id + ".EXE"); 
} 
4
6/19/2012 9:03:34 PM

Popular Answer

To use the AddToCustomer Downloads function, you must create an instance of the Customer Downloads class and supply it as a parameter. The error notice informs you that you are giving a FormCollection object to a function whose interface expects a Customer Downloads object.



Related Questions





Related

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