2012-06-19 71 views
1

我有一个表在我的数据库是这样的:插入新记录到数据库中使用实体框架

[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, 

id被定义主键。

我想插入一个新的记录到这张表中。这是我无法上班的代码。

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

我得到的错误是在线dbcd.AddToCustomer_Downloads(collection);

的最好重载方法匹配 'CustomerPortalMVC.Models.CustomerPortalEntities_Customer_Downloads.AddToCustomer_Downloads(CustomerPortalMVC.Models.Customer_Downloads)' 有一些无效参数

参数 '1':不能从“的System.Web转换。 Mvc.FormCollection”到 ‘CustomerPortalMVC.Models.Customer_Downloads’

什么我需要改变,以使这项工作?

回答

4

您需要提供CustomerPortalMVC.Models.Customer_Downloads类型的对象方法AddToCustomer_Downloads,然后调用SaveChanges您的数据的上下文是这样的:

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"); 
} 
1

您需要创建Customer_Downloads类的实例并将其传递给AddToCustomer_Downloads方法。该错误消息告诉你,该方法的接口需要一个Customer_Downloads对象,但是你将它传递给一个FormCollection对象。

+0

男人,我知道这是愚蠢的东西简单,由于是工作! –

相关问题