C# 使用实体框架将新记录插入数据库
我的数据库中有这样一个表:C# 使用实体框架将新记录插入数据库,c#,entity-framework,asp.net-mvc-2,C#,Entity Framework,Asp.net Mvc 2,我的数据库中有这样一个表: [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, pu
[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,
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");
}
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_Downloadscollection行上
与“CustomerPortalMVC.Models.CustomerPortalEntities\u Customer\u Downloads.AddToCustomer\u Downloads StomerPortalMVC.Models.Customer\u Downloads”匹配的最佳重载方法
有一些无效的参数
参数“1”:无法从“System.Web.Mvc.FormCollection”转换为
'CustomerPortalMVC.Models.Customer_下载'
我需要更改什么才能使其生效?您需要创建Customer\u Downloads类的实例,并将其传递给AddToCustomer\u Downloads方法。错误消息告诉您,该方法的接口需要Customer\u Downloads对象,但您正在向其传递FormCollection对象。您需要创建Customer\u Downloads类的实例,并将其传递给AddToCustomer\u Downloads方法。错误消息告诉您,该方法的接口需要一个Customer\u Downloads对象,但您正在向它传递一个FormCollection对象。您需要提供一个CustomerPortalMVC.Models.Customer\u类型的对象下载到方法AddToCustomer\u Downloads,然后在数据上下文上调用SaveChanges,如下所示:
[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,
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");
}
您需要提供CustomerPortalMVC.Models.Customer\u Downloads类型的对象,以方法AddToCustomer\u Downloads,然后在数据上下文上调用SaveChanges,如下所示:
[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,
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");
}
老兄,我知道这是一件愚蠢的事,一个简单的,谢谢你的工作!老兄,我知道这是一件愚蠢的事,一个简单的,谢谢你的工作!